我想刪除重複的元素,因此迭代通過ArrayList並比較兩個連續的元素。 (人具有可比性)Java:NoSuchElementException迭代通過ArrayList
ArrayList<Person> persons = getHelper().findAllPersons();
Collections.sort(persons);
ListIterator<Person> it = persons.listIterator();
if(it.hasNext()) {
Person tmp = it.next();
while(it.hasNext()) {
if(tmp.getLastDiscovered() == it.next().getLastDiscovered()) {
getHelper().delete(tmp);
}
tmp = it.next();
}
}
我得到tmp = it.next();
一個NoSuchElementException不應該while(it.hasNext())
阻止?
'決勝盤 unqiuePeople =新TreeSet的(人)'會做你在一行中想要的東西。 –
2013-05-10 14:53:11
使用適當定義的比較器/等於()方法 – 2013-05-10 14:57:00
@BrianAgnew假設OP已經在使用'Collections.sort'來訂購物品,我假定這已經定義。 – 2013-05-10 15:01:20