# Help comparing two arrays

M

#### Miss Michelle. Heigardt

Hallo, I have written this. It prints "false" and I do not know why. I
think it should print "true". If you can help I would thank you.
Thank you
Michelle

public static void main(String[] args)
{
int i[][]=new int;
i=4;
int j[][]=new int;
j=4;
System.out.println(java.util.Arrays.equals(i,j));
}

Here is something I copied from here saying it should print "true".

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#equals(byte[], byte[])

public static boolean equals(int[] a,
int[] a2)

Returns true if the two specified arrays of ints are equal to one
another. Two arrays are considered equal if both arrays contain the
same number of elements, and all corresponding pairs of elements in the
two arrays are equal. In other words, two arrays are equal if they
contain the same elements in the same order. Also, two array references
are considered equal if both are null.

Parameters:
a - one array to be tested for equality.
a2 - the other array to be tested for equality.
Returns:
true if the two arrays are equal.

K

#### Knute Johnson

Miss said:
Hallo, I have written this. It prints "false" and I do not know why. I
think it should print "true". If you can help I would thank you.
Thank you
Michelle

public static void main(String[] args)
{
int i[][]=new int;
i=4;
int j[][]=new int;
j=4;
System.out.println(java.util.Arrays.equals(i,j));
}

Here is something I copied from here saying it should print "true".

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#equals(byte[], byte[])

public static boolean equals(int[] a,
int[] a2)

Returns true if the two specified arrays of ints are equal to one
another. Two arrays are considered equal if both arrays contain the
same number of elements, and all corresponding pairs of elements in the
two arrays are equal. In other words, two arrays are equal if they
contain the same elements in the same order. Also, two array references
are considered equal if both are null.

Parameters:
a - one array to be tested for equality.
a2 - the other array to be tested for equality.
Returns:
true if the two arrays are equal.

Michelle:

You actually have the answer right there. What you have is an array
whose components are arrays of ints. Not an array of ints as required
by the method Arrays.equals(int[] a, int[] a2).

Change the last line of your program to:

System.out.println(java.util.Arrays.equals(i,j));

and it will print true.

P

#### Patricia Shanahan

Miss said:
Hallo, I have written this. It prints "false" and I do not know why. I
think it should print "true". If you can help I would thank you.
Thank you
Michelle

public static void main(String[] args)
{
int i[][]=new int;
i=4;
int j[][]=new int;
j=4;
System.out.println(java.util.Arrays.equals(i,j));
}

Here is something I copied from here saying it should print "true".

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#equals(byte[], byte[])

public static boolean equals(int[] a,
int[] a2)

Returns true if the two specified arrays of ints are equal to one
another. Two arrays are considered equal if both arrays contain the
same number of elements, and all corresponding pairs of elements in the
two arrays are equal. In other words, two arrays are equal if they
contain the same elements in the same order. Also, two array references
are considered equal if both are null.

Parameters:
a - one array to be tested for equality.
a2 - the other array to be tested for equality.
Returns:
true if the two arrays are equal.

int i[][] is not an array of int. It is an array of references to arrays
of int, so you were looking at the wrong part of the documentation.

To compare, for example, i to j, Arrays.equals will use
(i==null ? j==null : i.equals(j)), which is false unless
i and j are either both null or are references to the same object.

A lot of Java books confuse two different concepts:

1. Arrays of array references, which Java does have.

2. Multidimensional arrays, which it does not have and for which arrays
of array references are a rough approximation.

If i and j were two-dimensional arrays of int, they would have the same
size, and the same elements, in the same order, so they should be
equal. They are not equal because they are really arrays of references
to different arrays of int.

Patricia