int b [] = new int [a.length];
System.arraycopy (a, 0, b, 0, a.length) ;/ / put a copy to b on
/ * The following two comparison, the results are different, is how reason ? * /
System.out.println (a.equals (b)) ;/ / output : false
System.out.println (Arrays.equals (a, b)) ;/ / output : true
- ---- Solution -------------------------------------------- < br> System.out.println (Arrays.equals (a, b)) ;/ / output : true
compare the contents of two arrays
System.out.println (a.equals (b)) ;/ / output : false
compare the two array objects
can view the api documentation ah
------ Solution ------------------------------- -------------
1, Arrays.equals (a, b), this statement of equals, it is to determine whether the value of the two arrays are the same . Apparently identical .
2, a.equals (b), where the equals is a reference to determine the address of the two arrays are equal . Distinctly different .
3, a.equals (b), if a, b are of type String , then this equals determine whether the value is the same . In the JDK , String type from the Object class overrides the inherited equals method , method also changed and this need to pay attention !
------ Solution ---------------------------------------- ----
in java array is an object , the object is not an array equals the method for rewriting , so the array object in the call equals the time , using the equals method of Object , view source code, it can be seen , Object 's equals method is as follows :
public boolean equals (Object obj) {
return (this == obj);
}
Obviously , Object euqals object returns a result, the two reference objects are the same , that is, these two objects is an object. In this procedure , you create two objects , it is clear that even if the same element values Talia is definitely not an object, so the return is certainly false.
And Arrays.equals (a, b) source as follows:
public static boolean equals (int [] a, int [] a2) {
if (a == a2)
return true;
if (a == null | | a2 == null)
return false;
int length = a.length;
if (a2.length! = length)
return false;
for (int i = 0; i
return false;
return true;
}
According to source , it is clear if the two arrays are the same length , and the value of the corresponding element in the same position , the return is true. Therefore, this procedure Arrays.equals (a, b) return is certainly true.
------ For reference only -------------------------------------- -
Thank you god pointing
没有评论:
发表评论