Write a Java program to reverse an array of integer, e.g. [1,3,8,6] -> [6,8,3,1]
时间: 2024-03-10 13:48:44 浏览: 67
Sure, here's a Java program to reverse an array of integers:
```java
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 3, 8, 6};
int[] reversedArr = reverseArray(arr);
System.out.println(Arrays.toString(reversedArr));
}
public static int[] reverseArray(int[] arr) {
int[] reversedArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
reversedArr[arr.length - i - 1] = arr[i];
}
return reversedArr;
}
}
```
This program defines a function `reverseArray` that takes an array of integers as input and returns a new array with the elements in reverse order. The `main` function creates an array with the values `[1, 3, 8, 6]`, calls the `reverseArray` function to reverse the array, and then prints the reversed array using the `Arrays.toString` method.
阅读全文