Question
Intro to JAVA please see below thank you! ------------------------------------------------------------------------------------------ Now suppose we want to find the positions of all matches. Here is a plan, using
Intro to JAVA please see below thank you!
------------------------------------------------------------------------------------------
Now suppose we want to find the positions of all matches. Here is a plan, using the findNext method of the preceding problem:
Allocate a partially filled array result. While findNext returns a valid position Insert the position to the end of result. Copy result into an array whose length equals the number of matches. Return that copy.
Complete the following code:
import java.util.Arrays;
public class FindAll { /** Finds the positions of all occurrences of an element in an array. @param values an array of values @param searchedValue the value to search for @param the positions of all matches */ int[] findAll(int[] values, int searchedValue) { int[] result = new int[. . .]; int resultSize = 0; int pos = -1; do { pos = findNext(values, searchedValue, pos + 1); if (. . .) { . . . } } while (. . .); . . . }
/** Finds the next occurrence of an element in an array. @param values an array of values @param searchedValue the value to search for @param start the position at which to start the search @return the position of the first match at position >= start, or -1 if the element was not found */ int findNext(int[] values, int searchedValue, int start) { for(int position = start; position
if(values[position]==searchedValue)
return position; } return -1; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started