Win a copy of Penetration Testing Basics this week in the Security forum!

# Comparing 2 vectors, am I efficent?

Peter Primrose
Ranch Hand
Posts: 755
is there a better (more efficent way) to compate 2 vectors then the solution below?
this is the problem: I have a vector A with names [say 100 names] and another vector B with 8 names (all of them are subset of vector V)

i need to know WHERE are they located on vector A (the way they are ordered is very importent) so in other words if "James" is found in vector B I want to know what is its index in vector A.

Thanks

A=v
B=recordVector

int [] index = new int[100];
for (int i=0; i<100; i++)
{
index[i]= -1 ;
}

if( ! recordVector.isEmpty() )
{
for (int x=0; x<recordVector.size(); x++)
{
for (int i=0; i<v.size(); i++)
{
if (v.get(i).toString().equals(recordVector.get(x).toString()))
{
index[x]=i;
}
}
}
}

Amit Saini
Ranch Hand
Posts: 280
cant you do something like this:

1.loop over vector B with 8 elements
2. check if A 'contains()' element from vector B
3. if yes, return 'indexOf()' element in A
4. if no, just proceed with the loop.

Does it make sense? I hope Im understanding your problem correctly !

Peter Primrose
Ranch Hand
Posts: 755
yes, your answer makes sense, however, I'm not sure it will be more efficent.
I think that you have the same solution as O(N*M)

what do you think?

Amit Saini
Ranch Hand
Posts: 280
I think it will be more efficient.

In your code, you loop over B first. (8 elements)
After that you do another for loop and loop over A (100 elements) and compare each element of A to B to check if they are equal.

In my solution, the inner for loop is avoided and is instead replaced by standard Vector API, contains() and indexOf(). Now I dont know what internal algorithm is used for contains() and indexOf().

But yeah, we're doing almost the same thing, I agree.

Peter Primrose
Ranch Hand
Posts: 755
you know what, I'll use your solution - it more elegant :-)
thanks

Amit Saini
Ranch Hand
Posts: 280
glad i could be of any help

rahul V kumar
Ranch Hand
Posts: 82
I was thinking why not create a hashmap of your main vector A.

Then all you have to do is just loop through your vector B and look in that hashmap.

Peter Primrose
Ranch Hand
Posts: 755
well, sometimes I might add or MOVE items to the vector , and the oreder I enter them is important (that's my index). I suspect that hash will not help me as the order in which you enter them is important (mostly when you have a collision).
in the vector case I can simple 'null' the element.

Do you think my observation is correct?

rahul V kumar
Ranch Hand
Posts: 82
why dont you use arraylists instead of vector.
you will find better performance with arraylist than vector.
[ October 21, 2004: Message edited by: rahul V kumar ]

rahul V kumar
Ranch Hand
Posts: 82
Originally posted by rahul V kumar:
I was thinking why not create a hashmap.

Then all you have to do is just loop through your vector B and look in that hashmap.

Amit Saini
Ranch Hand
Posts: 280
Yups. I concur with Rahul. Vector methods are serialized and so its a performance hit. ArrayLists dont have serialized methods and so are supposed to be faster. How much of a difference will it make to your application, i dont know! So if you dont need synchronization, ArrayList is a good choice. Even if you do need synchronization with ArrayLIst, you could use this function, public static List synchronizedList(List list);

http://java.sun.com/docs/books/tutorial/collections/index.html

Peter Primrose
Ranch Hand
Posts: 755
thanks, I'll try that. from what I've read it looks good.

thanks agine.

Joel McNary
Bartender
Posts: 1840
amitdsaini :

Welcometo JavaRanch! Please help us abide by our naming convention by adding a few spaces to your display name here. (We are looking for both a first and a last name, separated by spaces, in the display names on JavaRanch.

Thanks!

Joel