From d7fb2edf0cdb0c5ae1ec5c6e8162bc23c5f893c6 Mon Sep 17 00:00:00 2001 From: 8ga Date: Fri, 10 Jan 2025 11:14:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20ArrayReverse.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArrayReverse.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ArrayReverse.java diff --git a/ArrayReverse.java b/ArrayReverse.java new file mode 100644 index 0000000..7c7fc99 --- /dev/null +++ b/ArrayReverse.java @@ -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 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); + } +}