Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

Id probably just get more upset. Its bett er to just drop it.

Answered: 1 week ago

Question

2. Do you find change a. invigorating? b. stressful? _______

Answered: 1 week ago

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago