public static void main(String[] args) { int[] arrays = {1, 2, 3, 4, 5}; // JDK1.5之后,没有下标 for (int array : arrays) { System.out.print(array + " "); } }
运行结果:1 2 3 4 5
public static void main(String[] args) { int[] arrays = {1, 2, 3, 4, 5}; printArray(arrays); } // 打印数组元素 public static void printArray(int[] arrays) { for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i] + " ") } }
运行结果:1 2 3 4 5
public static void main(String[] args) { int[] arrays = {1, 2, 3, 4, 5}; int[] reverse = reverse(arrays); for(int i = 0; i < reverse.length; i++) { System.out.print(reverse[i] + ""); } } // 反转 public static int[] reverse(int[] arrays) { int[] result = new int[arrays.length]; // 反转操作 for (int i = 0, j = result.length - 1; i < arrays.length; i++, j--) { result[j] = arrays[i]; } return result; }
运行结果:5 4 3 2 1