Question: Write a method with the header public static int indexOf(int[] arr1, int[] arr2) that takes two arrays containing sequences of integers and that returns the

Write a method with the header

public static int indexOf(int[] arr1, int[] arr2) 

that takes two arrays containing sequences of integers and that returns the index of the first occurrence of the first sequence in the second sequence, or -1 if the first sequence does not appear in the second sequence. For example, suppose that you have these arrays:

int[] list1 = {1, 3, 6}; int[] list2 = {1, 3, 5, 8, 12, 1, 3, 17, 1, 3, 6, 9, 1, 3, 6}; 

then the call indexOf(list1, list2) should return 8 because the sequence of values stored in list1 appears in list2 starting at index 8. Notice that list1 actually appearstwice in list2, starting at position 8 and starting at position 12. Your method should return the first such position.

If the first sequence is not contained in the second sequence, then the method should return -1. For example, if list2 had the same values as before but list1 stored {12, 1, 3, 6}, then the call indexOf(list1, list2) should return -1 because list1 is not contained in list2.

Special case: If either parameter is null or has a length of 0, the method should throw anIllegalArgumentException.

Important: You must not modify the original arrays.

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!