Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4171916

myArray.equals() does not check content equality

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.2.0
    • specification
    • generic
    • generic



      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)
      ======================================================================

            jjb Josh Bloch
            clucasius Carlos Lucasius (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: