1 public class Array02 { 2 public static void main(String[] args) { 3 int[] arr01 = {1,2,3,4,5}; 4 // 将arr01中的每一个值分别赋值给arr , 该for只能适合运用遍历输出。 5 for (int arr: arr01){ 6 System.out.print(arr+ " "); 7 } 8 System.out.println("\n"+"-------------------------------"); 9 //调用一下输出 10 arrPrint(arr01); 11 System.out.println("\n"+"-------------------------------"); 12 //调用一下翻转 13 int[] reserve = reserve(arr01); 14 arrPrint(reserve); 15 } 16 //定义一个输出方式 17 public static void arrPrint(int[] arr){ 18 for (int i = 0; i < arr.length; i++) { 19 System.out.print(arr[i] + " "); 20 } 21 } 22 //翻转数组 23 public static int[] reserve(int[] arrr){ 24 int[] result = new int[arrr.length]; 25 for (int i = 0; i < arrr.length; i++) { 26 result[i] = arrr[arrr.length-i-1]; 27 } 28 return result; 29 } 30 }
结果:
1 2 3 4 5 ------------------------------- 1 2 3 4 5 ------------------------------- 5 4 3 2 1 Process finished with exit code 0