1

I'm trying to implement the equals method in my class.. notice: the '_data' property is an double[][] array im trying to compare between the two objects.

anyway it compiles and everything but i always get a false answer and this can't be because both arrays are same :o

am i doing something wrong? is there any other easy way? (only with using equals from object class!!)

My code (JAVA):

public boolean equals(Object obj) {
    if (!(obj instanceof MyClass)) {
        return false;
    }

    MyClass myObj = (MyClass) obj;
    return this._data.equals(myObj._data);
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Popokoko
  • 6,413
  • 16
  • 47
  • 58
  • you have to paste a bigger code snippet. right now it's hard to judge what's causing problems. – soulcheck Dec 07 '11 at 23:00
  • 5
    Have you tried [`java.util.Arrays.deepEquals()`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html)? – Tomasz Nurkiewicz Dec 07 '11 at 23:01
  • For debugging purposes, would you please do something like `MyClass mc1 = new MyClass(); MyClass mc2 = mc1; boolean toCheck = mc1.equals(mc2);` Does toCheck == true? – Cody S Dec 07 '11 at 23:01
  • Tomasz Nurkiewicz - I can't use the method u suggested, i only asked for equals method implementation.. – Popokoko Dec 07 '11 at 23:18
  • believe in Java - the arrays are not the same (not the same instance)... in this case `equals` is testing if both are the same instance, not comparing the contents. – user85421 Dec 07 '11 at 23:19
  • @Cody S When doing what u suggested mc1.equals(mc2) it causing a problem because each time the equals method is triggered it means it is going to the same method (meaning its a recursive method that never ends..) this is why i call the equals method as posted here. – Popokoko Dec 07 '11 at 23:21
  • @Carlos Heuberger - im pretty positive that i can compare these arrays with 'equals' method but it looks like im doing something wrong.. – Popokoko Dec 07 '11 at 23:23
  • 1
    Im sure that if you use `equals` it is the same as using `==`, that is, only comparing if they are the same instance (the same reference), not if they have the same contents. Test it: `int[] a = { 1 }; int[] b = { 1 }; System.out.println(a.equals(b));` – user85421 Dec 08 '11 at 08:47
  • Mico0, Carlos is spot on. Your understanding is incorrect. – duffymo Dec 08 '11 at 10:53

5 Answers5

8

Use

Arrays.deepEquals(this._data, myObj._data) 

to make the test on the arrays.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
  • Thanks but i asked only by using equals method.. (Not array.equals()) – Popokoko Dec 07 '11 at 23:17
  • 2
    Seriously, when three people tell you to do it a certain way that's built right in to the standard library, and you're objecting to it based on "I can't use that", you need to justify why not. – Karl Knechtel Dec 07 '11 at 23:40
  • If you don't want to use the standard library, do a loop iterating on both dimensions. But that's horrible. – Baptiste Wicht Dec 08 '11 at 08:38
5

Use Arrays.deepEquals(data, myObj._data)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2

You can take a look at this post which describes the deepEquals method: Java Arrays.equals() returns false for two dimensional arrays

Community
  • 1
  • 1
Michaeldcooney
  • 1,435
  • 10
  • 14
0

I would override the equals method in your MyClass class.

@Override
public boolean equals(Object obj) {//if you insist in using an Object as argument instead of double[][]
   if (!(obj instanceof MyClass)) {
      return false;
   }

   MyClass myObj = (MyClass) obj;
   if(_data.length == myObj._data.length){
      for(int i=0; i<_data.length; i++){
         if(_data[i].length == myObj._data[i].length){
            for(int j=0; j<_data[i].length; j++){
               if(_data[i][j] != myObj._data[i][j]){
                  return false;
               }
            }
         }else{
            return false;
         }
      }
   }else{
      return false;
   }
   return true;
}

This code considers the case you will have a two dimensional array but not a square matrix. i.e. first row with three elements, second row with 27 elements, third row with N elements... For example, a test case:

    double[][] first= new double[5][3];
    double[][] second= new double[5][3];
    for(int i=0; i<first.length; i++){
        for(int j=0; j<first[i].length; j++){
            first[i][j] = 5;
        }
    }
    for(int i=0; i<second.length; i++){
        for(int j=0; j<second[i].length; j++){
            second[i][j] = 5;
        }
    }
    second[4][2] = 2;

    MyClass c1 = new MyClass(first);
    MyClass c2 = new MyClass(second);

    System.out.println("Equals: "+c1.equals(c2));
cabreracanal
  • 924
  • 1
  • 14
  • 36
0

Joshua Bloch tells you how to implement equals and hashCode (they must be done in tandem) in chapter 3 of "Effective Java". That should tell you all you need to know in order to override equals for your class.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I believe the question is not about implementing `equals`, but how to compare arrays,, mostly why the `equals` method does not work with arrays! – user85421 Dec 08 '11 at 08:52
  • I thought it was about how to override equals in the OP's class so it could compare using array data members. Is that not correct? "I'm trying to implement the equals method in my class..." - as for the why, it's obvious: equals only performs "shallow equals" when it's not overridden. – duffymo Dec 08 '11 at 09:00
  • not so obvious for the OP, considering his comment "...im pretty positive that i can compare these arrays with 'equals'...", but I can be wrong at that. Actually I believe it is *less* than a "shallow equals" - he's just comparing references. – user85421 Dec 08 '11 at 09:59
  • That's what I'm calling "shallow equals". Deep equals would ensure that the arrays are the same length and all the elements are identical. It'll be harder with doubles; the best you can do is say that they're all within an epsilon error of each other. – duffymo Dec 08 '11 at 10:08