• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My loop has gone Array trying to detect identical arrays

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look in the Java Language Specification and find whether as array has an overridden equals() method.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic