public class TestEquals
1:
2: /**
3: A demonstration program to test two arrays for equality.
4: */
5: public class TestEquals
6: {
7: public static void main(String[] args)
8: {
9: int[] a = new int[3];
10: int[] b = new int[3];
11: setArray(a);
12: setArray(b);
13:
14: if (b == a)
15: System.out.println("Equal by ==.");
16: else
17: System.out.println("Not equal by ==.");
18:
19: if (equals(b, a))
20: System.out.println("Equal by the equals method.");
21: else
22: System.out.println("Not equal by the equals method.");
23: }
24:
25: public static boolean equals(int[] a, int[] b)
26: {
27: boolean elementsMatch = true; //tentatively
28: if (a.length != b.length)
29: elementsMatch = false;
30: else
31: {
32: int i = 0;
33: while (elementsMatch && (i < a.length))
34: {
35: if (a[i] != b[i])
36: elementsMatch = false;
37: i++;
38: }
39: }
40:
41: return elementsMatch;
42: }
43:
44: public static void setArray(int[] array)
45: {
46: for (int i = 0; i < array.length; i++)
47: array[i] = i;
48: }
49: }