数组元素反转:
class Hello2 { public static void main(String[] args) { int[] arr = {11,12,13,14,15}; reverseArray(arr); print(arr); } public static void reverseArray(int[] arr) { for (int i = 0;i < arr.length / 2 ;i++ ) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } } public static void print(int[] arr) { for (int i = 0;i < arr.length ;i++ ) { System.out.println(arr[i]); } } }
结果:
原文地址:https://www.cnblogs.com/Wangzui1127/p/11185260.html
时间: 2024-10-08 09:22:55