显示哪些元素从ArrayList中删除 [英] Show which elements are removed from Arraylist
本文介绍了显示哪些元素从ArrayList中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个非常简单的世界模拟器。每个时间段,人们年龄的增长,一旦他们达到最大值的年龄,他们死了。我已经成功地让人们死,将它们从的ArrayList
删除。不过,我想表明,人们每一次的步骤(这样的元素是从的ArrayList
删除)过程中死亡,但我不知道该怎么做。这是我的code:
的(迭代器<&人GT; personIterator = persons.iterator(); personIterator.hasNext();){
人人= personIterator.next();
如果(person.getAge()&GT = Person.MAX_AGE){
personIterator.remove();
}
}
解决方案
您可以积累您删除到另一个集合的元素,然后返回/打印/不管它。例如:
列表<&人GT;死亡=新的LinkedList<>();
对于(迭代器<&人GT; personIterator = persons.iterator(); personIterator.hasNext();){
人人= personIterator.next();
如果(person.getAge()&GT = Person.MAX_AGE){
deaths.add(人);
personIterator.remove();
}
}
I'm writing a very simple world simulator. Each time step, people get older, and once they reach a maximum age, they die. I already managed to let people die and to remove them from the ArrayList
. However, I would like to show which people died during each time step (so which elements are removed from the ArrayList
), but I don't know how to do it. This is my code:
for(Iterator<Person> personIterator = persons.iterator(); personIterator.hasNext();) {
Person person = personIterator.next();
if (person.getAge() >= Person.MAX_AGE){
personIterator.remove();
}
}
解决方案
You could accumulate the elements you remove to another collection, and then return/print/whatever it. E.g.:
List<Person> deaths = new LinkedList<>();
for(Iterator<Person> personIterator = persons.iterator(); personIterator.hasNext();) {
Person person = personIterator.next();
if (person.getAge() >= Person.MAX_AGE) {
deaths.add(person);
personIterator.remove();
}
}
这篇关于显示哪些元素从ArrayList中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文