Question: Java Coding Question: I Have the following code: public class Hello{ public static boolean matchChecker(int[] match, int[] mainArray){ for (int i = 0, j =

Java Coding Question:

I Have the following code:

public class Hello{

public static boolean matchChecker(int[] match, int[] mainArray){

for (int i = 0, j = 0; i < mainArray.length; i++) {

if (array[i] == match[j]) {

if (++j >= match.length)

return true;

}

else {

i -= j;

j = 0;

}

}

return false;

}

public static void main(String[]args){

System.out.println(matchChecker(new int[]{1,2,3,4}, new int[]{1,2,3,4,5,6}));

}

}//closes class

what the code does is it takes 2 integer arrays. If the match array is found in the main array it returns true. If the match array is not found in the main array it returns false.

The match array must be found in the exact same consecutive order or it returns false.

How would I change the code so instead of returning true or false it returns the index value of when the match starts?

For example if I have match = {1,2,3} and mainArray = {5,4,3,3,1,2,3,7,9]

The program should return int 4. Because that is the index position where the match starts in the main array. If there is no match the program returns -1

Thanks in advance for help. Absolutely no helper classes can be added.

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!