How do I remove current element from list while iterating using for loop
?
如何在迭代使用for循环时从列表中删除当前元素?
I have below a list containing:
我有以下清单,包括:
List<String> list = new ArrayList<String>();
list.add("a");
list.add("a2");
list.add("a3");
list.add("a4");
list.add("a5");
list.add("a6");
list.add("a7");
list.add("a8");
list.add("a9");
list.add("a10");
list.add("a11");
list.add("a12");
list.add("a13");
list.add("a14");
list.add("a15");
List<List<String>> parts = chopped(list, 4);
System.out.println(parts);
which gives me output below like this:
得到如下输出:
[[a, a2, a3, a4], [a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
Now I am trying to remove first list from the list in below program:
现在我正在尝试从下面程序的列表中删除第一个列表:
for(int i=0; i<parts.size(); i++){
System.out.println("Loop number: "+i);
System.out.println("Current list: "+parts);
Thread.sleep(2000);
parts.remove(i);
System.out.println("After removing: "+parts);
}
Here I am using parts.remove(i);
to remove current element, it gives me the following output:
这里我使用的是part .remove(I);为了去除当前元素,输出如下:
[[a, a2, a3, a4], [a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
Loop number: 0
Current list: [[a, a2, a3, a4], [a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
After removing: [[a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
Loop number: 1
Current list: [[a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
After removing: [[a5, a6, a7, a8], [a13, a14, a15]]
This one removes first current element [a, a2, a3, a4]
which is correct but in running second loop it removes [a9, a10, a11, a12]
.
这一个去掉了第一个电流元素[a, a2, a3, a4],这是正确的,但是在运行第二个循环时,它去掉了[a9, a10, a11, a12]。
I want to remove it loop wise like below:
我想把它删除循环如下:
[[a, a2, a3, a4], [a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
[[a5, a6, a7, a8], [a9, a10, a11, a12], [a13, a14, a15]]
[[a9, a10, a11, a12], [a13, a14, a15]]
[[a13, a14, a15]]
[]
Edited:
编辑:
When I try to access List inside List using parts.get(loopNumber)
or parts.get(i)` it throws an exception error:
当我尝试使用part .get(loopNumber)或part .get(I)访问List时,它会抛出一个异常错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at gmailSendLaptopGUI.MyProgram.main(MyProgram.java:156)
3 个解决方案
#1
3
The problem is that you are editing the list and at the same time reading it.
问题是你在编辑列表的同时也在阅读它。
You have to decrease the counter when you remove an object from the list.
当您从列表中删除一个对象时,您必须减少计数器。
for(int i=0; i<parts.size(); i++){
System.out.println("Loop number: "+i);
System.out.println("Current list: "+parts);
Thread.sleep(2000);
parts.remove(i--);
System.out.println("After removing: "+parts);
}
#2
1
One more way to do is this:
还有一种方法是:
int loopNumber = 0;
while (!parts.isEmpty()) {
System.out.println("Loop" + loopNumber);
parts.remove(0);
loopNumber++;
System.out.println(parts);
}
Not sure why are you putting Thread.sleep().
不知道为什么要输入Thread.sleep()。
#3
1
You have already fine answers but I think that if you want to keep a for
statement, a simple and readable way is not specifying increment (i++
) in the ForUpdate
part of the for
statement as it is not mandatory and you always want to suppress the current first element (parts.remove(0)
).
你已经好答案,但我想,如果你想保持一个声明,一个简单的和可读的方式不是指定增量(+ +)ForUpdate for语句的一部分,因为它不是强制性的,你总是想抑制当前第一个元素(parts.remove(0))。
for(int i=0; i<parts.size(); ){
System.out.println("Current list: "+parts);
Thread.sleep(2000);
parts.remove(0);
System.out.println("After removing: "+parts);
}
#1
3
The problem is that you are editing the list and at the same time reading it.
问题是你在编辑列表的同时也在阅读它。
You have to decrease the counter when you remove an object from the list.
当您从列表中删除一个对象时,您必须减少计数器。
for(int i=0; i<parts.size(); i++){
System.out.println("Loop number: "+i);
System.out.println("Current list: "+parts);
Thread.sleep(2000);
parts.remove(i--);
System.out.println("After removing: "+parts);
}
#2
1
One more way to do is this:
还有一种方法是:
int loopNumber = 0;
while (!parts.isEmpty()) {
System.out.println("Loop" + loopNumber);
parts.remove(0);
loopNumber++;
System.out.println(parts);
}
Not sure why are you putting Thread.sleep().
不知道为什么要输入Thread.sleep()。
#3
1
You have already fine answers but I think that if you want to keep a for
statement, a simple and readable way is not specifying increment (i++
) in the ForUpdate
part of the for
statement as it is not mandatory and you always want to suppress the current first element (parts.remove(0)
).
你已经好答案,但我想,如果你想保持一个声明,一个简单的和可读的方式不是指定增量(+ +)ForUpdate for语句的一部分,因为它不是强制性的,你总是想抑制当前第一个元素(parts.remove(0))。
for(int i=0; i<parts.size(); ){
System.out.println("Current list: "+parts);
Thread.sleep(2000);
parts.remove(0);
System.out.println("After removing: "+parts);
}