【发布时间】:2019-03-21 20:12:20
【问题描述】:
我在通过比较找到最小值的元素并将其放入另一个向量中排序向量时遇到了一些麻烦,该向量将使用两个周期进行排序,特别是我一直有 ArrayIndexOutOfBoundsException。
Vector<Figure> myList = new Vector<Figure>(); //this is the vector with all the unsorted geometric shapes
Vector<Figure> listordered = new Vector<Figure>(); //the vector where i want to put them sorted
Figure x = null;
int indice = 0;
System.out.println(myList.size());
do{
for(int i=0;i<myList.size();i++) {
x = myList.get(i);
if(x.compareTo(MIN) <0)
MIN=x;
indice = myList.indexOf(MIN);
}
listordered.add(MIN);
myList.removeElementAt(indice);
System.out.println(myList.size());
}while(myList.size()!=0);
System.out.println(listordered);
我的想法是用一个循环找到最小值,然后将其添加到排序的向量中,并在另一个循环中继续这样做,直到第一个向量中没有更多元素,并在每次找到新的最小元素时删除。但它不起作用。
【问题讨论】:
-
有什么理由不能使用
Vector.sort(Comparator<? super E> c)? (如果线程安全不是问题,您是否考虑过使用ArrayList而不是Vector?) -
A) 请阅读minimal reproducible example 并相应地加强您的问题 B) 请参阅stackoverflow.com/questions/5554734/…
标签: java vector indexoutofboundsexception cycle