Java Reference
In-Depth Information
System.out.println("]");
}
}
12.
To make these methods process arrays of
String
s, you must change
int[]
to
String[]
and replace all other occurrences of the word
int
with
String
. You must
also change any comparisons using
==
to comparisons using the
equals
method.
13.
public static boolean allLess(int[] list1, int[] list2) {
if (list1.length != list2.length) {
return false;
}
for (int i = 0; i < list1.length; i++) {
if (list1[i] >= list2[i]) {
return false;
}
}
return true;
}
14.
The method to swap array elements works because, unlike integers, arrays are
objects and use reference semantics. This means that changes to an array parame-
ter's elements will be seen in the original array by the caller.
15.
2 [0, 0, 1, 0]
1 [0, 0, 1, 0]
3 [0, 0, 1, 1]
2 [0, 0, 1, 1]
16.
2 [0, 1]
1 [0, 1]
1 [1, 2]
0 [1, 2]
17.
public static void swapPairs(int[] list) {
for (int i = 0; i < list.length - 1; i += 2) {
swap(list, i, i + 1);
}
}
18.
{20, 30, 40, 50, 60, 70, 80, 90, 100, 100}
19.
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
20.
a1: {26, 19, 14, 11, 10}
a2: {1, 4, 9, 16, 25}
21.
a1: {1, 3, -3, 13, -4, -24, -6, -14}
a2: {1, 1, 2, 3, 5, 8, 13, 21}
Search WWH ::
Custom Search