Skip to content

Commit 9170146

Browse files
authoredApr 21, 2018
Update 03_Foreach.md
1 parent a4d3e56 commit 9170146

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed
 

‎ch01/03_Foreach.md

+41-39
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
在这里,我们的代码也是计算整数列表的总和
77

88
```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;
1315
```
1416

1517
第三行中的循环被称为 `foreach` 循环,即使它是用关键字。 它相当于以下内容:
1618

1719
```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+
}
2224
```
2325

2426
强调的代码对应于用户编写的内容,编码器以系统的方式添加失码代码。 它引入了 `Iterator<Integer>` 类型的变量来迭代 `List<Integer>` 类型的列表整型。 通
@@ -29,54 +31,54 @@ for (Iterator<Integer> it = ints. iterator(); it.hasNext(); ) {
2931
定义了 `iterator``hasNext``next`,它们被 `foreach` 循环的翻译使用(迭代器也有一个方法 `remove`,它不被翻译使用):
3032

3133
```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+
}
4042
```
4143

4244
集合框架中的所有集合,集合和列表都实现了 `Iterable<E>` 接口; 而其他供应商或用户定义的类也可以实现它。`foreach` 循环也可以应用于一个数组:
4345

4446
```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+
}
5052
```
5153

5254
`foreach` 循环是故意保持简单的,只捕获最常见的情况。如果你想使用 `remove` 方法或者并行迭代多个列表,你需要明确地引入一个迭代器。这是一个从
5355
`Double` 列表中删除负值的方法:
5456

5557
```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+
}
6163
```
6264

6365
这里是计算两个向量的点积的方法,表示为 `Double` 列表,两个长度都相同。 给定两个向量:
6466
`u1, … , un``v1, … , vn` 他们计算: u1 * v1> + … + un * vn:
6567

6668
```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+
}
8082
```
8183

8284
两个迭代器 `uIt``vIt` 在锁定步骤中跨越列表 `u``v`。 循环条件只检查第一个迭代器,但断言确认我们可以有使用第二个迭代器,因为我们先前测试了两

0 commit comments

Comments
 (0)
Please sign in to comment.