可以使用以下方法實現Java數組的逆序輸出數字:
int[] array = {1, 2, 3, 4, 5};
int[] reverseArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
reverseArray[array.length - 1 - i] = array[i];
}
for (int i = 0; i < reverseArray.length; i++) {
System.out.println(reverseArray[i]);
}
完整代碼如下所示:
public class ReverseArray {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int[] reverseArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
reverseArray[array.length - 1 - i] = array[i];
}
for (int i = 0; i < reverseArray.length; i++) {
System.out.println(reverseArray[i]);
}
}
}
運行以上代碼,將會輸出以下結果:
5
4
3
2
1