如何避免以下代码中的Java.util.IllegalStateException?
我有一个包含重复值的整数列表。我需要做的是找到重复的整数,添加它们的值,然后通过删除找到的重复项将结果添加到列表中。这就是我正在做的:
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(5);
list1.add(3);
list1.add(5);
list1.add(4);
List<Integer> list2 = new ArrayList<Integer>();
Iterator<Integer> it = list1.iterator();
while (it.hasNext()) {
Integer int1 = it.next();
if (list2.isEmpty()) {
list2.add(int1);
it.remove();
} else {
ListIterator<Integer> it2 = list2.listIterator();
while (it2.hasNext()) {
Integer int2 = it2.next();
if (int2 != int1) {
it2.add(int1);
it.remove();// I get exception here
} else {
it2.remove();
it.remove();
Integer newint = int1 + int2;
it2.add(newint);
}
}
}
}
for(Integer in : list2){
System.out.println(in);
}
输出应该如下所示
<代码>2
10
3
4
感谢您的宝贵时间。
I have a List of integer's with duplicate values in it. What I need to do is find the duplicate integers, add their value and then add the result to the list by removing the duplicates found. Here is what I am doing:
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(5);
list1.add(3);
list1.add(5);
list1.add(4);
List<Integer> list2 = new ArrayList<Integer>();
Iterator<Integer> it = list1.iterator();
while (it.hasNext()) {
Integer int1 = it.next();
if (list2.isEmpty()) {
list2.add(int1);
it.remove();
} else {
ListIterator<Integer> it2 = list2.listIterator();
while (it2.hasNext()) {
Integer int2 = it2.next();
if (int2 != int1) {
it2.add(int1);
it.remove();// I get exception here
} else {
it2.remove();
it.remove();
Integer newint = int1 + int2;
it2.add(newint);
}
}
}
}
for(Integer in : list2){
System.out.println(in);
}
Output should look like2
10
3
4
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如其他海报所说,迭代时无法删除。尽管有一些“技巧”,但在迭代时弄乱集合肯定会导致奇怪的运行时错误。
不管怎样,你在解决这个问题上太过努力了。
这是一个快速但肮脏的解决方案,其中包含一小部分代码:
As the other posters have said, you can't remove while iterating. Even though there are 'tricks', messing with a collection while iterating is a surefire way to get weird runtime bugs.
Anyway, you are working way too hard on the problem.
Here's a quick and dirty solution with a fraction of the code:
如果您被允许使用 Map,您可以做这样简单的事情(传入伪代码):
您的结果是 m 的值(这是一个 Collection)。
编辑:你说你有 LatLng 而不是 Integers - 我不知道 LatLng,但在快速谷歌之后我会尝试以下内容,假设你想“添加”你的 LatLng要点:
我在这里看到的唯一问题是这个
m.containsKey(x)
取决于equals
的正确实现,在阅读 这个If you are allowed to use a Map you could do something simple like this (incoming pseudocode):
Your result are the values of m (which is a Collection).
Edit: You said you have LatLng instead of Integers - I don't know LatLng but after a quick google I'd take a shot at the following, assuming that you want to "add" up your LatLng points:
The only problem I can see here is that this
m.containsKey(x)
depends on the correct implementation ofequals
, which I'm not sure after reading this这是因为你删除了同一个元素两次。第一次出现在
if(list2.isEmpty())
中(因为list2
在开头和紧随其后的else
主体中为空。It is because you remove the same element twice. First time in
if(list2.isEmpty())
(becauselist2
is empty in the beginning and immediately after that in theelse
body.从
remove
方法的文档中:From the documentation for the
remove
method:您不能两次删除当前元素。你需要重新思考你的逻辑。
You cannot remove the current element twice. You need to rethink your logic.