Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes compare data in 2 vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "compare data in 2 vectors" Watch "compare data in 2 vectors" New topic
Author

compare data in 2 vectors

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 477

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 ?


Thanks,
Sherif
Mudassir Rahat
Greenhorn

Joined: Oct 14, 2006
Posts: 3
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.


Mudassir -- �Diplomacy is the art of saying "Nice doggie" until you can find a rock.�
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 477

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

Joined: Oct 14, 2006
Posts: 3
Yes,As vectors can be equal only if they refer to the same object.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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 ]

Regards, Rene Larsen
Dropbox Invite
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: compare data in 2 vectors
 
Similar Threads
JTable ClassCastException...
JTable
Vector within vector
Vectors of vectors vs vectors of beans for storing data, advantages and disavantages
DefaultTableModel and Vector