United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4171916 myArray.equals() does not check content equality
4171916 : myArray.equals() does not check content equality

Details
Type:
Enhancement
Submit Date:
1998-09-08
Status:
Closed
Updated Date:
1998-09-17
Project Name:
JDK
Resolved Date:
1998-09-17
Component:
specification
OS:
generic
Sub-Component:
language
CPU:
generic
Priority:
P3
Resolution:
Duplicate
Affected Versions:
1.2.0
Fixed Versions:

Related Reports
Duplicate:
Relates:

Sub Tasks

Description

Name: clC74495			Date: 09/08/98


given:

Object[] a = ...
Object[] b = ...

a.equals(b) is today equivalent to
a == b

This is rather useless.  I don't need any special
help to do a == b :-)

Javasoft's solution to this problem is to provide
the new Array.equals(a,b), but it does _not_ work
for multidimensional arrays.

Consider the definition of Array.equals(): it
compares if two arrays are equal by executing
a[i].equals(b[i]) for each element i of the
array.  This works great for single-dimensional
arrays, but suppose a and b are defined as:

Object[][] a = ...
Object[][] b = ...

a[i] and b[i] are themselves arrays, but
since arrays do not override Object.equals(),
a[i].equals(b[i]) will check identity equality,
not content equality, for a[i] and b[i].

There are more issues here than just testing
for content equality of multidimensional arrays.
For example, the Collection.contains() method
will fail if passed an array, because
Collection.contains() uses equals().  As I
suspect arrays also don't override hashCode(),
their usefulness in any sort of collection seems
limited to the most mundane Vector applications.

I hope Javasoft will consider fixing this
problem, if not for jdk 1.2 for the following
release.  Fixing it soon, however, presents
certain advantages; leaving it until after the
jdk 1.2 release would require you to deprecate
a number of functions under the new class
Arrays.  IMHO, there is no reason why the class
Arrays need exist, period; why should one not
be able to say:

int x[] = new int[10];
x.fill(0);
x[2] = 2;
x.sort();

But I'll settle for a working equals() :-)

Thanks,

Evan
(Review ID: 37606)
======================================================================

                                    

Comments
EVALUATION

It is true that the current definitions don't help with multidmensional arrays. On the other hand, the user can code this up directly. I'm not convinced either way. Forwarding to Josh for his comments.

gilad.bracha@eng 1998-09-08

   I agree that this would have been desirable.  It might have been possible in 1.1, but it's no longer possible.  It's an incompatible langague change.
Sigh...

joshua.bloch@Eng 1998-09-17
                                     
1998-09-17
WORK AROUND

Use Arrays.AsList.  This gets you everything (hashing, printing, equality checking, multidimensional arrays).  It doesn't work for primitives types, and there's a performance penalty associated with it.

joshua.bloch@Eng 1998-09-17
                                     
1998-09-17



Hardware and Software, Engineered to Work Together