用Java写一个数组反转的代码
时间: 2024-05-02 09:21:40 浏览: 105
Java实现数组反转翻转的方法实例
以下是Java代码实现数组反转:
```
public class ArrayReverse {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Original array: " + Arrays.toString(arr));
reverseArray(arr);
System.out.println("Reversed array: " + Arrays.toString(arr));
}
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--;
}
}
}
```
输出结果:
```
Original array: [1, 2, 3, 4, 5]
Reversed array: [5, 4, 3, 2, 1]
```
阅读全文