GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes My loop has gone Array trying to detect identical arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My loop has gone Array trying to detect identical arrays" Watch "My loop has gone Array trying to detect identical arrays" New topic
Author

My loop has gone Array trying to detect identical arrays

lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
I am trying to detect if two arrays via user input are identical or not. My output keeps saying not identical. Obviously I have an error, however I am not locating it. I'm not needing to compare the length as the arrays are already set to be 3x3 just compare input for equal value.

Updated code: still not coming out correctly. I attempted to sort and I also did the .equals but my code is still not identifying identical arrays. feedback is greatly appreciated!

Abdulla S Mamuwala
Greenhorn

Joined: Jul 27, 2009
Posts: 21
This statement - "if(m1==m2)" is checking for references to your arrays m1, m2. It is not really checking for the contents of the arrays.
Shashank Gollapudi
Ranch Hand

Joined: Jan 19, 2012
Posts: 33
As Abdulla mentioned, you are just comparing the references,

One good thing i observed is you have overridden equals method, but using == ( reference equality) will not cause overridden equals to run, use .equals() instead

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2303
    
  49
When using code tags remember to highlight your code before clicking on the 'Code' button. I've edited your post this time so the code tags enclose your code.

You may want to look at the java.util.Arrays API docs as there is a method in that class which may help you achieve what you want.
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
Tony Docherty wrote:When using code tags remember to highlight your code before clicking on the 'Code' button. I've edited your post this time so the code tags enclose your code.

You may want to look at the java.util.Arrays API docs as there is a method in that class which may help you achieve what you want.


Thank you for your assistance and explanation of how to post my code
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
Tony Docherty wrote:When using code tags remember to highlight your code before clicking on the 'Code' button. I've edited your post this time so the code tags enclose your code.

You may want to look at the java.util.Arrays API docs as there is a method in that class which may help you achieve what you want.


Thank you. I think I understand what you are saying. I read up a bit on the .equals and on sort . Unfortunately, I am still missing something.
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
Shashank Gollapudi wrote:As Abdulla mentioned, you are just comparing the references,

One good thing i observed is you have overridden equals method, but using == ( reference equality) will not cause overridden equals to run, use .equals() instead


Thank you. I read up on that and see the difference between them. greatly appreciated. now if I can just find out why my code is off. Thanks again for you assistance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
You should look in the Java Language Specification and find whether as array has an overridden equals() method.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2303
    
  49
I read up a bit on the .equals and on sort . Unfortunately, I am still missing something.

Can you post the code you tried along with the arrays you were testing for equality.
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
Tony Docherty wrote:
I read up a bit on the .equals and on sort . Unfortunately, I am still missing something.

Can you post the code you tried along with the arrays you were testing for equality.


I actually updated/edited it and put it in replace of my original post. (I wasn't sure if I was suppose to edit it or post a new update into here)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
lish McDonald wrote:. . . (I wasn't sure if I was suppose to edit it or post a new update into here)
You should always post a new post. It makes the thread incomprehensible if you appear to be replying to something which has been changed.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2303
    
  49
I actually updated/edited it and put it in replace of my original post

You are still making the same mistake of comparing an array for equality with another array using ==. This just tests if they are the same array object and not if the contents are the same.
Remember you are passing in arrays of arrays so when you do m1[i] you are asking for the array at index i of the m1 array of arrays variable. If you want to compare the arrays long hand (ie not using the method I suggested earlier) then you need another loop so you compare each element in this array with each element in the other array ie if ( m1[i][j] == m2[i][j] ). Remember, before you run the loop do simple checks such as if both arrays are the same object then they must be equal, if the arrays are not the same length they can't be equal etc.
 
GeeCON Prague 2014
 
subject: My loop has gone Array trying to detect identical arrays