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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!