我想删除重复的元素,因此遍历 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();
}
}
我得到一个 NoSuchElementExceptiontmp = it.next();
不应该while(it.hasNext())
阻止吗?