File tree Expand file tree Collapse file tree 1 file changed +7
-7
lines changed Expand file tree Collapse file tree 1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change 88考虑工具类 ` java.util.Collections ` 中的反向方法,它接受任何类型的列表并将其反向。它可以是以下两个签名中的任何一个,它们是相同的:
99
1010``` 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);
1313```
1414
1515通配符签名稍短且更清晰,是类库中使用的签名。如果使用第二个签名,则很容易实现该方法:
1616
1717``` 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 ));
2322 }
23+ }
2424```
2525
2626这会将参数复制到临时列表中,然后以相反的顺序将副本写回到原始文件中。如果您尝试使用类似方法体的第一个签名,它将不起作用:
You can’t perform that action at this time.
0 commit comments