-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEqualsArray.qhelp
More file actions
52 lines (43 loc) · 2.31 KB
/
EqualsArray.qhelp
File metadata and controls
52 lines (43 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
The <code>equals</code> and <code>hashCode</code> methods on arrays only consider object identity, not
array contents, which is unlikely to be what is intended.
</p>
</overview>
<recommendation>
<p>To compare the lengths of the arrays and the corresponding pairs of elements in the arrays, use
one of the comparison methods from <code>java.util.Arrays</code>:</p>
<ul>
<li>The method <code>Arrays.equals</code> performs a shallow comparison. That is, array elements
are compared using <code>equals</code>.</li>
<li>The method <code>Arrays.deepEquals</code> performs a deep comparison, which is appropriate
for comparisons of nested arrays.</li>
</ul>
<p>
Similarly, <code>Arrays.hashCode</code> and <code>Arrays.deepHashCode</code> can be used to compute
shallow and deep hash codes based on the hash codes of individual array elements.
</p>
</recommendation>
<example>
<p>In the following example, the two arrays are first compared using the <code>Object.equals</code> method.
Because this checks only reference equality and the two arrays are different objects, <code>Object.equals</code>
returns <code>false</code>. The two arrays are then compared using the <code>Arrays.equals</code> method.
Because this compares the length and contents of the arrays, <code>Arrays.equals</code> returns
<code>true</code>.
</p>
<sample src="EqualsArray.java" />
</example>
<references>
<li>Java API Documentation:
<a href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#equals%28java.lang.Object[],%20java.lang.Object[]%29">Arrays.equals</a>,
<a href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object[],%20java.lang.Object[]%29">Arrays.deepEquals</a>,
<a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#equals%28java.lang.Object%29">Object.equals</a>,
<a href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#hashCode%28java.lang.Object[]%29">Arrays.hashCode</a>,
<a href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepHashCode%28java.lang.Object[]%29">Arrays.deepHashCode</a>,
<a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode%28%29">Object.hashCode</a>.</li>
</references>
</qhelp>