• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Arrays - equals method

 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3851
  • Mark post as helpful
  • send pies
  • 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: 3851
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Seriously? That's what you're going with? I prefer this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic