Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vector.clear() problem

Status
Not open for further replies.

tqtd

Programmer
Nov 17, 2007
1
GB
i have two vectors a and b.
i need to write elements from vector b that are at the same index as equal elements in vector a.
if in vector a elements a(0) and a(5) are equal i write b(0) and b(5) into another vector.

for (int i=0, n=a.size(); i<n; i++){
onenode = (String)a.get(i);
if (!visited.contains((String)a.get(i))){
for (int j=0, m=a.size(); j<m; j++){
secondnode = (String) a.get(j);
if (secondnode.equals(onenode)){
selectednets.add((String)b.get(j));
}
}
visited.add(onenode);
}
// selectednets.removeAllElements();
}
if i uncoment vector clear i get empty vector and if i leave it all elements of b are stored in the selectednets vector.
can anybody explane what am i doing wrong?
 
Hey, you've probably already figured this out, but it looks like you're covering the same items when you do your search.
In a three-element vector you would compare: a == b, a == c.

Because you're starting the inner loop from zero, you're comparing: a == a, a == b, a == c, b == a, b == b, b == c, c == a, c == b, c == c.

You have an outer loop from 0:size-1 and an inner loop the same. The inner loop should probably start from i+1 instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top