• 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

Arrays - equals method

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String[][] s1 = {{"a"},{"b"}}

String[][] s2 = {{"a"},{"b"}}

System.out.println(s1.equals(s2));
// return false , because s1 & s2 are reference that are refering to array of String not to String , so equals() method will work like == because no overriding ...

Arrays.equals(s1,s2);
// why this is returning false ???

please help me ..
thanks a lot .
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just have to apply the first reason you specify, again to the second comparison.

When you say Array.equals(Object[], Object[]), the return value is true, if the objects in the array are objects. But when you say Array.equals(Object[][], Object[][]), you are not going to compare objects in the array, you are going to compare arrays, which will return false because "because s1 & s2 are reference that are refering to array of String not to String , so equals() method will work like == because no overriding...". To illustrate my point, here is the same program with some more outputs...
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too good Agni ...
probably I am working too much ... now I am going home for nice sleep ..
good night ...
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaa one more thing ..

String[][] s1 = {{"a"},{"b"}}
String[][] s2 = {{"a"},{"b"}}
Arrays.deepEquals(s1,s2);

now this will return true , right ??
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have created a two dimensional (2 X 1) array. In Java, these are one dimensional arrays of one dimensional arrays.

Method java.util.Arrays.equals() is comparing the two rows,
{"a"} == {"a"} // false
{"b"} == {"b"} // false
because they are different array objects and they are being compared with the Object.equals() method.

If you want to compare the underlying elements of a multidimensional array, use java.util.Arrays.deepEquals() in Java 5. It will do what you want.

 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic