indexOf
Category: Programming
Author: Stuart Reges
Book Chapter: 7.4
Problem: indexOf
Write a static method indexOf that takes two
arrays of integers and that returns the index of the first occurrence of
the first list in the second list or -1 if the first list does not appear
in the second list. For example, suppose that you have two integer arrays
called list1 and list2 that store the following values:
list1: (1, 3, 5, 8, 12, 1, 3, 17, 1, 3, 6, 9, 1, 3, 6)
list2: (1, 3, 6)
then the call:
indexOf(list2, list1)
should return 8 because the sequence of values stored in list2 appears in
list1 starting with index 8. Notice that list2 actually appears twice in
list1, starting at position 8 and starting at position 12. Your method is
to return the first such position.
If the second list is not contained in the first list, then the method
should return the value -1. For example, if list1 had the same value as
before but list2 stored (12, 1, 3, 6), then the call indexOf(list2, list1)
should return -1 because list2 is not contained in list1. If the first
list is empty, your method should return 0.
Write your solution to indexOf below.