添加 ArrayReverse.java
This commit is contained in:
commit
d7fb2edf0c
21
ArrayReverse.java
Normal file
21
ArrayReverse.java
Normal file
@ -0,0 +1,21 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayReverse {
|
||||
// ArrayList 反转元素
|
||||
// 双指针解法
|
||||
// 出自 java.util.Collections#reverse
|
||||
public static void main(String[] args) {
|
||||
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
int size = list.size();
|
||||
Integer x, y;
|
||||
for (int begin = 0, mid = size >> 1, end = size - 1; begin < mid; begin++, end--) {
|
||||
x = list.get(begin);
|
||||
y = list.get(end);
|
||||
list.set(begin, y);
|
||||
list.set(end, x);
|
||||
}
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user