Write a static method named contains that accepts two arrays of integers a1 and a2 as parameters and that returns a boolean value indicating whether or not a2's sequence of elements appears in a1 (true for yes, false for no). The sequence of elements in a2 may appear anywhere in a1 but must appear consecutively and in the same order. For example, if variables called list1 and list2 store the following values:
int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8}; int[] list2 = {1, 2, 1};
Then the call of
contains(list1, list2)
shouldreturn true
because list2's sequence of values {1, 2, 1} is contained in list1 starting at index 5. If list2 had stored the values{2, 1, 2}
, the call ofcontains(list1, list2)
wouldreturn false
because list1 does not contain that sequence of values. Any two lists with identical elements are considered to contain each other, so a call such ascontains(list1, list1)
should return true.You may assume that both arrays passed to your method will have lengths of at least 1. You may not use any Strings to help you solve this problem, nor methods that produce Strings such as
Arrays.toString
.
I'm looking for general feedback to improve my future code. I'm intent on learning Java, so any style tips would be well appreciated. Other ways to accomplish this task?
public static boolean contains(int[] list1, int[] list2) {
for (int i = 0; i <= (list1.length - list2.length); i++) {
if (list1[i] == list2[0]) {
for (int j = 1; j < list2.length; j++) {
if (list1[i + j] == list2[j]) {
if (j == (list2.length - 1)) {
return true;
}
} else {
break;
}
}
}
}
return false;
}
i = 0
will never be less than(list1.length - list2.length)
, meaning that this code will correctlyreturn false
. The question also states to test "whether or not a2's sequence of elements appears in a1 (true for yes, false for no). " \$\endgroup\$