从列表中删除多个元素同时进行迭代。

huangapple 未分类评论72阅读模式
英文:

Remove multiple elements from list while iterating

问题

如果我需要遍历一个列表并尝试将元素分组在一起,最好的方法是什么?

假设例如我们有以下要遍历的对象:

People
  |---Jimmy
  |     -Country: Canada
  |---Johnny
  |     -Country: Ireland
  |---Vinny
  |     -Country: Italy
  |---Tommy
  |     -Country: Ireland
  |---Wendy
  |     -Country: Canada

我需要遍历每个人,并根据相同国家创建一个组。

例如,从Jimmy开始,遍历列表以查找在加拿大的任何人,只有Wendy与相同的国家匹配,因此它们会被分在一组。当迭代到下一个人时,我们不希望再次为Wendy执行相同的过程(因为她已经分组)。因此,我们需要将她从原始列表中移除。

我已经阅读到,对于从活动迭代列表中删除元素,使用迭代器是最佳方法。但是迭代器只会移除当前元素。还有其他建议吗?我在考虑使用while循环,然后再使用for循环进行迭代匹配。

例如:

while (!originalPersonList.isEmpty()) {
  ArrayList<Person> newGroup = new ArrayList<Integer>();
  List<Integer> indexToRemove = new ArrayList<Integer>();
  newGroup.add(0);
  indexToRemove.add(0);
  for (int i=1; i < originalPersonList.size(); i++) {
    if (originalPersonList.get(0).getCountry() == originalPersonList.get(i).getCountry()) {
       indexToRemove.add(i);
       newGroup.add(i);
    }
  }
  originalPersonList.removeAll(indexToRemove);
}

这是我要走的方向,但我觉得可能有更优雅的解决方案。

抱歉,我觉得我应该在开始时强调这一点。我受限于Java 7。

英文:

If I need to iterate over a list and try to group elements together, what is the best way to go about this?

Let's say for example we have the following objects to loop through:

People
  |---Jimmy
  |     -Country: Canada
  |---Johnny
  |     -Country: Ireland
  |---Vinny
  |     -Country: Italy
  |---Tommy
  |     -Country: Ireland
  |---Wendy
  |     -Country: Canada

I need to go through each person, and create a group with people in the same country.

For example, it starts with Jimmy, iterates through the list to find anyone in Canada, only Wendy matches the same country so they both get grouped together. When iterating to the next person, we don't want to go through the same process for Wendy (as she is already grouped). So we need to remove her from the original list.

I've read that the iterator is best for removing elements from an active iterating list. However that only removes the current element. Is there any other suggestions? I was thinking a while loop and then a for loop which iterates to match.

For example:

while (!originalPersonList.isEmpty()) {
  ArrayList&lt;Person&gt; newGroup = new ArrayList&lt;Integer&gt;();
  List&lt;Integer&gt; indexToRemove = new ArrayList&lt;Integer&gt;();
  newGroup.add(0);
  indexToRemove.add(0);
  for (int i=1; i &lt; originalPersonList.size(); i++) {
    if (originalPersonList.get(0).getCountry() == originalPersonList.get(i).getCountry()) {
       indexToRemove.add(i);
       newGroup.add(i);
    }
  }
  originalPersonList.removeAll(indexToRemove);
}

This was the direction I was going towards, but I feel there is a more elegant solution out there.

Apologies - I feel I should have highlighted this at the start. I'm restricted to Java 7.

huangapple
  • 本文由 发表于 2020年8月14日 23:32:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/63415785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定