Looping through two Vectors

I have two vectors (Vector 1 and Vector2)containing various objects. I need an algoritm which will loop through the two vectors and create a third vector containing all the objects in Vector 1 that are not in Vector two...Thanking you in advance...
[262 byte] By [java_swing_dudea] at [2007-10-2 6:36:16]
# 1
did you ever look at the Vector class to see whether it offers a method for that?
ChristianMennea at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 2

I have tried this but I am having problems

while (iter.hasNext()) {

System.out.println("Av Task Data " + iter.next());

UserTask task = (UserTask) iter.next();

if (selectedTasksData.contains(iter.next())){

nonSelectedAvailableTasksData.add(iter.next());

}

}

java_swing_dudea at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 3
Be aware of two things. One thing is that each time you call "next()", it steps one object further. So you actually set through thre objects on each iteration. The other thing is that IIRC Vector has a method that does exactly what you want, so read the API.
ChristianMennea at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 4

Your problem is that next() winds on the cursor in the vector: it returns the next object every time.

eg To test and add something:

Object o = i.next();

if (secondList.contains(o))

{

thirdList.add(o);

}

itchyscratchya at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 5

public class VectorCopy{

Vector one;

Vector two;

Vector copy;

copyProcess(copy, one, two);

copyProcess(copy, two, one);

public void copyProcess(Vector copy, Vector source, Vector check){

for(int i = 0; i < source.size(); i++){

if( ! check.contains(source.get(i)) ){

copy.add(source.get(i));

}

}

}

}

TuringPesta at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 6
Iterator iter = availableTasksData.iterator();while(iter.hasNext()){ String o = (String)iter.next(); if (!selectedTasksData.contains(o)){nonSelectedAvailableTasksData.add(o);}}this does not seem to return anything
java_swing_dudea at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 7
If it doesn't return anything, then you probably need to override the "equals" method in your class to compare values correctly. As mentioned by Chris above, you can do this in one line if you read the Vector API. You do not need a loop.
MLRona at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 8
are you guys all talking about .retains()?bc. i think he wants the opposite of it.also in my code take outcopyProcess(copy, two, one);i didnt realize the OP only wanted elements of Vector one.
TuringPesta at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 9
> are you guys all talking about .retains()?> bc. i think he wants the opposite of it.Yes. It's called "removeAll()".
ChristianMennea at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 10

> are you guys all talking about .retains()?

> bc. i think he wants the opposite of it.

There is already an opposite of retainAll. If he doesn't want to actually modify Vector1, then he can copy the Vector (copy constructor or clone() method), then use the opposite of retainAll.

MLRona at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 11
that stings.in my defense im working with only one eye this morning.
TuringPesta at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 12
> that stings.> in my defense im working with only one eye this> morning.is it yours, at least?
dmbdmba at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...
# 13

>> is it yours, at least?

tip of the day: dont leave contacts stewing in some perverted

salt/alcohol mix overnight (mistaking it for Bausch&Lomb

all-in-one) and then put one into your eye

i described it to a coworker this morning as: 100% satisfying

what i imagined my eyeball exploding inside my head would feel like.

needless to say its all gimpy and i have to wear my glasses...

which is pretty much like looking through frosted glass, lol.

TuringPesta at 2007-7-16 13:38:48 > top of Java-index,Java Essentials,Java Programming...