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