我想删除重复的元素,因此迭代通过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