Justin's Words

Java Iterator and ListIterator

本文包含的 ArrayList 用法可在上篇文章找到,先上代码:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class IteratorExample {
public static void main(String args[]) {

ArrayList list = new ArrayList();

list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");

System.out.println("Iterator using while loop: ");
Iterator it1 = list.iterator();
while (it1.hasNext()) {
System.out.println("Next element: " + it1.next());
}

System.out.println("Iterator using for loop: ");
for (Iterator it2 = list.iterator(); it2.hasNext();) {
System.out.println("Next element: " + it2.next());
}

System.out.println("List iterator (forward iteration): ");
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
System.out.println("Next element: " + lit.next());

}

System.out.println("List iterator (backward iteration): ");
lit = list.listIterator(list.size());
while (lit.hasPrevious()) {
System.out.println("Previous element: " + lit.previous());

}

lit.next();
lit.add("One more element");
System.out.println("Modified list after the insertion of the new element");

System.out.println("Index of next element: " + lit.nextIndex());
System.out.println("Index of previous element: " + lit.previousIndex());

for (lit = list.listIterator(); lit.hasNext();) {
System.out.println("Next element: " + lit.next());
}

lit.previous();
lit.remove();

System.out.println("Modified list after the removal of an element");
for (lit = list.listIterator(); lit.hasNext();) {
System.out.println("Next element: " + lit.next());
}

}
}

运行结果:

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
31
32
33
34
35
36
37
38
39
Iterator using while loop:
Next element: One
Next element: Two
Next element: Three
Next element: Four
Next element: Five
Iterator using for loop:
Next element: One
Next element: Two
Next element: Three
Next element: Four
Next element: Five
List iterator (forward iteration):
Next element: One
Next element: Two
Next element: Three
Next element: Four
Next element: Five
List iterator (backward iteration):
Previous element: Five
Previous element: Four
Previous element: Three
Previous element: Two
Previous element: One
Modified list after the insertion of the new element
Index of next element: 2
Index of previous element: 1
Next element: One
Next element: One more element
Next element: Two
Next element: Three
Next element: Four
Next element: Five
Modified list after the removal of an element
Next element: One
Next element: One more element
Next element: Two
Next element: Three
Next element: Four

Iterator

Method

检查是否还有下一个元素和迭代到并获取下一个元素

检查是否还有下一个元素格式:

1
boolean hasNext();

迭代下一个元素并返回它格式(不存在则抛出 NoSuchElementException 异常):

1
E next();

使用:

1
2
3
4
5
6
7
8
9
10
System.out.println("Iterator using while loop: ");
Iterator it1 = list.iterator();
while (it1.hasNext()) {
System.out.println("Next element: " + it1.next());
}

System.out.println("Iterator using for loop: ");
for (Iterator it2 = list.iterator(); it2.hasNext();) {
System.out.println("Next element: " + it2.next());
}

移除当前元素

格式(不存在抛出 IllegalStateException 异常):

1
void remove();

用法:

1
it.remove();

ListIterator

Method

查看是否有下一个元素和返回它

查看是否有下一个元素, 格式:

1
boolean hasNext();

迭代到下一个元素并返回它, 格式:

1
E next();

使用:

1
2
3
4
5
System.out.println("List iterator (forward iteration): ");
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
System.out.println("Next element: " + lit.next());
}

查看是否有上一个元素并返回它

查看是否有上一个元素,格式:

1
boolean hasPrevious();

迭代到上一个元素并返回它,格式:

1
E previous();

使用:

1
2
3
4
5
System.out.println("List iterator (backward iteration): ");
lit = list.listIterator(list.size());
while (lit.hasPrevious()) {
System.out.println("Previous element: " + lit.previous());
}

给当前位置添加新元素

原来在该位置的元素索引加 1,一直类推到最后一个元素。

格式:

1
void add(E e);

使用:

1
lit.add("One more element");

获取前一位置的索引并迭代到前一位置

格式:

1
int previousIndex();

使用:

1
System.out.println("Index of previous element: " + lit.previousIndex());

获取后一位置的索引并迭代到后一位置

格式:

1
int nextIndex();

使用:

1
System.out.println("Index of next element: " + lit.nextIndex());

移除当前位置上的元素

格式:

1
void remove();

使用:

1
lit.remove();

替换当前位置上的元素

格式:

1
void set(E e);

使用:

1
lit.set("Modified element")

使用内置的迭代方法

格式:

1
void forEachRemaining(Consumer<? super E> action)

使用:

1
2
3
lit.forEachRemaining(n -> {
System.out.println(n);
});