java
复制代码
for(int i = 0;i < arr.length /2;i++){
string temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i -1] = temp;
}
for(int i = 0,j = arr.length - 1;i < j;i++,j--){
string temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
java
复制代码
public class ArrayReverse {
public static void reverse(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--;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("原始数组: " + Arrays.toString(arr));
reverse(arr);
System.out.println("颠倒后的数组: " + Arrays.toString(arr));
}
}