1. 使用额外的数组:
public static int[] reverseArray(int[] arr) {int[] result = new int[arr.length];for (int i = 0; i < arr.length; i++) {result[i] = arr[arr.length - 1 - i];}return result;}
2. 使用原地反转:
public static void reverseArray(int[] arr) {int start = 0;int end = arr.length - 1;while (start < end) {int temp = arr[start];arr[start] = arr[end];arr[end] = temp;start++;end--;}}
3. 使用`Collections.reverse()`方法(适用于对象数组):
import java.util.Arrays;import java.util.Collections;public static void reverseArray(Integer[] arr) {Collections.reverse(Arrays.asList(arr));}
4. 使用栈进行反转:
import java.util.Stack;public class Test {public static void main(String[] args) {String[] array = {"a", "b", "c", "d"};Stackstack = new Stack<>(); for (String s : array) {stack.push(s);}while (!stack.isEmpty()) {System.out.println(stack.pop());}}}
5. 使用`StringBuilder`进行反转:
public class Test {public static void main(String[] args) {String[] array = {"a", "b", "c", "d"};StringBuilder sb = new StringBuilder();for (String s : array) {sb.append(s);}String reversed = sb.reverse().toString();System.out.println(reversed);}}
以上方法均可实现数组位置的反转。您可以根据具体需求选择合适的方法

