Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Equals...

Status
Not open for further replies.

Hattusas

Programmer
Nov 11, 2001
344
TR
String[][] salih = new String[15][15];
salih[0][0]="Petronas";
String[][] seyhan = new String[15][15];
seyhan[0][0] ="Petronas";

salih.equals(seyhan) return false.
How can I override this?
Thanks.(I need to compare these 2 variables,not their values one by one.)

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
The reason why false is returned is because you are comparing the references and not the acutal strings. If you did the following:

salih[0][0].equals(seyhan[0][0])

It should return true because you are actually comparing the values not the addresses given by the references.
 
ah, but I think that Hattusas is saying 'how can I compare the contents of two 2D arrays and say whether their contents are equal'....you could create a class containing an array and override the equals() method to check that 1) they are the same dimensions, 2) the contents of each cell are the same.

There is probably a more efficient answer, like using lists that can be directly compared, rather than stepping through each cell.

Jeremy Nicholson, Director of a UK-based Java and Data Warehousing consultancy
 
>> Thanks.(I need to compare these 2 variables,not their values one by one.)

that is what your code does so it works exactly as you asked. Since the two variable are not equal it returns false.

-pete
 
if you want the two values to return true, you have to overwrite both the equal and hashcode methods. Else, it will always return false. This is a contract.

~za~
You can't bring back a dead thread!
 
Thanks for your replies.I see that we are one the same side.I'll have to compare the variables one by one (that means I won't use String dimension in my solution B-) )

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question
equals 1
Replies
3
Views
110
Replies
1
Views
93

Part and Inventory Search

Sponsor

Back
Top