Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions

Question

How do parent-infant attachment bonds form?

Answered: 1 week ago