java/ArrayReverse.java

22 lines
653 B
Java
Raw Normal View History

2025-01-10 11:14:41 +08:00
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);
}
}