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...
did you ever look at the Vector class to see whether it offers a method for that?
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());
}
}
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.
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);
}
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));
}
}
}
}
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
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 >

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.
> are you guys all talking about .retains()?> bc. i think he wants the opposite of it.Yes. It's called "removeAll()".
> 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 >

that stings.in my defense im working with only one eye this morning.
> that stings.> in my defense im working with only one eye this> morning.is it yours, at least?
>> 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.