6
6
在这里,我们的代码也是计算整数列表的总和
7
7
8
8
``` java
9
- List<Integer > ints = Arrays . asList(1 ,2 ,3 );
10
- int s = 0 ;
11
- for (int n : ints) { s += n; }
12
- assert s == 6 ;
9
+ List<Integer > ints = Arrays . asList(1 ,2 ,3 );
10
+ int s = 0 ;
11
+ for (int n : ints) {
12
+ s += n;
13
+ }
14
+ assert s == 6 ;
13
15
```
14
16
15
17
第三行中的循环被称为 ` foreach ` 循环,即使它是用关键字。 它相当于以下内容:
16
18
17
19
``` java
18
- for (Iterator<Integer > it = ints. iterator(); it. hasNext(); ) {
19
- int n = it. next();
20
- s += n;
21
- }
20
+ for (Iterator<Integer > it = ints. iterator(); it. hasNext(); ) {
21
+ int n = it. next();
22
+ s += n;
23
+ }
22
24
```
23
25
24
26
强调的代码对应于用户编写的内容,编码器以系统的方式添加失码代码。 它引入了 ` Iterator<Integer> ` 类型的变量来迭代 ` List<Integer> ` 类型的列表整型。 通
@@ -29,54 +31,54 @@ for (Iterator<Integer> it = ints. iterator(); it.hasNext(); ) {
29
31
定义了 ` iterator ` ,` hasNext ` 和 ` next ` ,它们被 ` foreach ` 循环的翻译使用(迭代器也有一个方法 ` remove ` ,它不被翻译使用):
30
32
31
33
``` java
32
- interface Iterable <E> {
33
- public Iterator<E > iterator ();
34
- }
35
- interface Iterator <E> {
36
- public boolean hasNext ();
37
- public E next ();
38
- public void remove ();
39
- }
34
+ interface Iterable <E> {
35
+ public Iterator<E > iterator ();
36
+ }
37
+ interface Iterator <E> {
38
+ public boolean hasNext ();
39
+ public E next ();
40
+ public void remove ();
41
+ }
40
42
```
41
43
42
44
集合框架中的所有集合,集合和列表都实现了 ` Iterable<E> ` 接口; 而其他供应商或用户定义的类也可以实现它。` foreach ` 循环也可以应用于一个数组:
43
45
44
46
``` java
45
- public static int sumArray(int [] a) {
46
- int s = 0 ;
47
- for (int n : a) { s += n; }
48
- return s;
49
- }
47
+ public static int sumArray(int [] a) {
48
+ int s = 0 ;
49
+ for (int n : a) { s += n; }
50
+ return s;
51
+ }
50
52
```
51
53
52
54
` foreach ` 循环是故意保持简单的,只捕获最常见的情况。如果你想使用 ` remove ` 方法或者并行迭代多个列表,你需要明确地引入一个迭代器。这是一个从
53
55
` Double ` 列表中删除负值的方法:
54
56
55
57
``` java
56
- public static void removeNegative(List<Double > v) {
57
- for (Iterator<Double > it = v. iterator(); it. hasNext();) {
58
- if (it. next() < 0 ) it. remove();
59
- }
60
- }
58
+ public static void removeNegative(List<Double > v) {
59
+ for (Iterator<Double > it = v. iterator(); it. hasNext();) {
60
+ if (it. next() < 0 ) it. remove();
61
+ }
62
+ }
61
63
```
62
64
63
65
这里是计算两个向量的点积的方法,表示为 ` Double ` 列表,两个长度都相同。 给定两个向量:
64
66
` u1, … , un ` 和 ` v1, … , vn ` 他们计算: u1 * v1> + … + un * vn:
65
67
66
68
``` java
67
- public static double dot(List<Double > u, List<Double > v) {
68
- if (u. size() != v. size())
69
- throw new IllegalArgumentException (" different sizes" );
70
- double d = 0 ;
71
- Iterator<Double > uIt = u. iterator();
72
- Iterator<Double > vIt = v. iterator();
73
- while (uIt. hasNext()) {
74
- assert uIt. hasNext() && vIt. hasNext();
75
- d += uIt. next() * vIt. next();
76
- }
77
- assert ! uIt. hasNext() && ! vIt. hasNext();
78
- return d;
79
- }
69
+ public static double dot(List<Double > u, List<Double > v) {
70
+ if (u. size() != v. size())
71
+ throw new IllegalArgumentException (" different sizes" );
72
+ double d = 0 ;
73
+ Iterator<Double > uIt = u. iterator();
74
+ Iterator<Double > vIt = v. iterator();
75
+ while (uIt. hasNext()) {
76
+ assert uIt. hasNext() && vIt. hasNext();
77
+ d += uIt. next() * vIt. next();
78
+ }
79
+ assert ! uIt. hasNext() && ! vIt. hasNext();
80
+ return d;
81
+ }
80
82
```
81
83
82
84
两个迭代器 ` uIt ` 和 ` vIt ` 在锁定步骤中跨越列表 ` u ` 和 ` v ` 。 循环条件只检查第一个迭代器,但断言确认我们可以有使用第二个迭代器,因为我们先前测试了两
0 commit comments