添加 ArrayReverse.java

This commit is contained in:
8ga 2025-01-10 11:14:41 +08:00
commit d7fb2edf0c

21
ArrayReverse.java Normal file
View 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);
}
}