Algorithm
JAVA : LinkedList , ArrayList 순환도중에 값 삭제하기
하이후에호
2020. 3. 6. 03:30
반응형
일반적으로 for 안에 arraylist를 순환하면서 삭제하게된다면 현재값이 사라지게되어 다음값을 참조할수 없게된다.
이럴 경우에는 Iterator를 이용하면 쉽다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package LineSweep;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(2);
list.add(3);
list.add(5);
list.add(7);
list.add(88);
list.add(12);
list.add(62);
list.add(52);
for(Iterator<Integer> it = list.iterator();it.hasNext();) {
Integer value = it.next();
if(value % 2 == 0)
}
for(Integer tmp : list) {
System.out.println(tmp);
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형