Skip to content

Commit 47fbe8f

Browse files
author
maskleo
authoredMar 21, 2018
格式化
1 parent b0deb32 commit 47fbe8f

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed
 

‎ch01/03_Foreach.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ interface Iterator<E> {
3939
}
4040
```
4141

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

4444
```java
4545
public static int sumArray(int[] a) {
46-
int s = 0;
47-
for (int n : a) { s += n; }
48-
return s;
49-
}
46+
int s = 0;
47+
for (int n : a) { s += n; }
48+
return s;
49+
}
5050
```
5151

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

5555
```java
@@ -65,17 +65,17 @@ public static void removeNegative(List<Double> v) {
6565

6666
```java
6767
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;
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;
7979
}
8080
```
8181

0 commit comments

Comments
 (0)