Use
As to why
For arrays,
Often we really want value equality for arrays, of course, which is why
Now consider this snippet:
In contrast, here's what
On
It's worth noting the analogy between these two methods and what we've discussed so far with regards to nested arrays.
If you want
@reference_1_stackoverflow.com
deepEquals(Object[], Object[])
.
Returns true
if the two specified arrays are deeply equal to one another.
Since an int[]
is an instanceof Object
, an int[][]
is an instanceof Object[]
.As to why
Arrays.equals
doesn't "work" for two dimensional arrays, it can be explained step by step as follows:
For arrays, equals
is defined in terms of object identity
System.out.println(
(new int[] {1,2}).equals(new int[] {1,2})
); // prints "false"
This is because arrays inherit their equals
from their common superclass, Object
.Often we really want value equality for arrays, of course, which is why
java.util.Arrays
provides the static
utility method equals(int[], int[])
.System.out.println(
java.util.Arrays.equals(
new int[] {1,2},
new int[] {1,2}
)
); // prints "true"
Array of arrays in Java
- An
int[]
is aninstanceof Object
- An
int[][]
is aninstanceof Object[]
- An
int[][]
is NOT aninstanceof int[]
java.util.Arrays.equals
is "shallow"
Now consider this snippet:System.out.println(
java.util.Arrays.equals(
new int[][] {
{ 1 },
{ 2, 3 },
},
new int[][] {
{ 1 },
{ 2, 3 },
}
)
); // prints "false"
Here are the facts:- Each argument is an
Object[]
- The element at index 0 is an
int[] { 1 }
- The element at index 1 is an
int[] { 2, 3 }
.
- The element at index 0 is an
- There are two
Object[]
instances - There are four
int[]
instances
Arrays.equals(Object[], Object[])
overload. From the API:ReturnsNow it should be clear why the above snippet printstrue
if the two specified arrays ofObjects
are equal to one another. The two arrays are consideredequal
if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objectse1
ande2
are considered equal if(e1==null ? e2==null : e1.equals(e2))
.
"false"
; it's because the elements of the Object[]
arrays are not equal by the above definition (since an int[]
has its equals
defined by object identity).
java.util.Arrays.deepEquals
is "deep"
In contrast, here's what Arrays.deepEquals(Object[], Object[])
does:Returnstrue
if the two specified arrays are deeply equal to one another. Unlike theequals(Object[],Object[])
method, this method is appropriate for use with nested arrays of arbitrary depth.
System.out.println(
java.util.Arrays.deepEquals(
new int[][] {
{ 1 },
{ 2, 3 },
},
new int[][] {
{ 1 },
{ 2, 3 },
}
)
); // prints "true"
On Arrays.toString
and Arrays.deepToString
It's worth noting the analogy between these two methods and what we've discussed so far with regards to nested arrays.System.out.println(
java.util.Arrays.toString(
new int[][] {
{ 1 },
{ 2, 3 },
}
)
); // prints "[[I@187aeca, [I@e48e1b]"
System.out.println(
java.util.Arrays.deepToString(
new int[][] {
{ 1 },
{ 2, 3 },
}
)
); // prints "[[1], [2, 3]]"
Again, the reasoning is similar: Arrays.toString(Object[])
treats each element as an Object
, and just call its toString()
method. Arrays inherit its toString()
from their common superclass Object
.If you want
java.util.Arrays
to consider nested arrays, you need to use deepToString
, just like you need to use deepEquals
.@reference_1_stackoverflow.com
No comments:
Post a Comment