Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare data in 2 vectors

 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I got 2 Vectors of vectors for instance
I need to compare the strings inside vOrgTable_1 and vBckTable_1 and so on till i reach the end of the big vector , i made the loop to get the data for these inner vectors but i need to know how to compare the data inside , what i should do to achieve this ? i used .equals() always giving me false although they got the same strings ,

Anyone got a recommendations ?
 
Mudassir Rahat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not compare the vectors themselves as they are different references and cannever be equal,if you want to compare the content,you will have to do it individually.Sorry there are no shortcuts.
 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mudassir Rahat:
Do not compare the vectors themselves as they are different references and cannever be equal,if you want to compare the content,you will have to do it individually.Sorry there are no shortcuts.


do you mean getting the data in every index for the inner vector and compare it ?
 
Mudassir Rahat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,As vectors can be equal only if they refer to the same object.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can modify the inner objects - you can/need to override the equals and hashCode methods.

Then should it be possible to call inner_object1.equals(inner_object2) - otherwise you need to do as Mudassir wrote.
[ December 31, 2008: Message edited by: Rene Larsen ]
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mudassir Rahat:
Yes,As vectors can be equal only if they refer to the same object.

You haven't looked at how Vector implements the equals method, have you?

Vector actually uses AbstractList's equals method (only synchronized). Here is that method:

So it returns true if all objects inside the vector are equal, and in the same order.

Consider this example:

So if equals is overridden you should be able to just use equals on the outer vectors; their elements will be compared using equals as well, etc. You could potentially have an unlimited vector depth, and just one call will be enough.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic