File tree 1 file changed +7
-7
lines changed
1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change 8
8
考虑工具类 ` java.util.Collections ` 中的反向方法,它接受任何类型的列表并将其反向。它可以是以下两个签名中的任何一个,它们是相同的:
9
9
10
10
``` java
11
- public static void reverse(List<?> list);
12
- public static < T > void reverse(List<T > list);
11
+ public static void reverse(List<?> list);
12
+ public static < T > void reverse(List<T > list);
13
13
```
14
14
15
15
通配符签名稍短且更清晰,是类库中使用的签名。如果使用第二个签名,则很容易实现该方法:
16
16
17
17
``` java
18
- public static < T > void reverse(List<T > list) {
19
- List<T > tmp = new ArrayList<T > (list);
20
- for (int i = 0 ; i < list. size(); i++ ) {
21
- list. set(i, tmp. get(list. size() - i - 1 ));
22
- }
18
+ public static < T > void reverse(List<T > list) {
19
+ List<T > tmp = new ArrayList<T > (list);
20
+ for (int i = 0 ; i < list. size(); i++ ) {
21
+ list. set(i, tmp. get(list. size() - i - 1 ));
23
22
}
23
+ }
24
24
```
25
25
26
26
这会将参数复制到临时列表中,然后以相反的顺序将副本写回到原始文件中。如果您尝试使用类似方法体的第一个签名,它将不起作用:
You can’t perform that action at this time.
0 commit comments