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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot remove element from vector

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
I want to make a simple selection sort to sort a small vector. The stuff in the vector has a boolean method called "isBefore" that I can use to do the sort. For some reason, my sort blows up when I do ".removeElementAt...".
Obviously it is trying to remove an element that is not present. However, I cannot see how that could happen! This is driving me insane, please help!


public static Vector sortVector(Vector a)
{
int i=0;
entry champ= new entry();
entry contender= new entry();
int champIndexValue=0;
Vector b = new Vector(100,100);
while(a.size()>0)
{
i=0;
champ=(entry)a.firstElement();
while(i<a.size())
{
contender= (entry) a.elementAt(i);
if(contender.isBefore(champ))
{
champ=contender;
champIndexValue=i;
}
i++;
}
b.add(champ);
a.removeElementAt(champIndexValue);
}
return b;
}
 
I'd recommend that you do your sort by implementing the java.util.Comparator class and using the java.util.Collections.sort(List list, Comparator c) method. (This will work because Vector implements List).

Hint: Your implementation of Comparator can make use of your isBefore() method.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top