QUESTION

Text
Image

INTRO TO JAVA please help fill in the (. . .)

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.

See Section 7.3.9 for copying an array. (Note that if there are no matches, the Arrays.copyOf method will yield an array of length 0.) Complete the following code.


FindAll.java 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) \{ // From preceding problem \} \}

Public Answer

NOSYGP The First Answerer