文章目录 遍历数组的三种方式1.for循环(1)遍历一维数组(2)遍历二维数组 2.foreach遍历(1).遍历一维数组(2).遍历二维数组 3.Arrays里的toString方法(1).遍历一维数组(2).遍历二维数组 遍历数组的三种方式 1.for循环 (1)遍历一维数组 int[] arr = {1,3,2,4,5}; for (int i =0;i<arr.length;i++){System.out.print(arr[i]);} (2)遍历二维数组 int[][] arr1 = {{1,2,3},{2,3,4},{3,4,5}}; for (int i =0;i<arr1.length;i++){for (int j =0;j<arr1[i].length;j++){System.out.print(arr1[i][j]+ " ");}System.out.println(); } 2.foreach遍历 (1).遍历一维数组 int[] arr = {1,3,2,4,5}; for (int arr2:arr){System.out.println(arr2); } (2).遍历二维数组 int[][] arr1 = {{1,2,3},{2,3,4},{3,4,5}}; for (int[] arr3:arr1){for (int arr4:arr3){System.out.print(arr4 +" ");}System.out.println(); } 3.Arrays里的toString方法 (1).遍历一维数组 int[] arr = {1,3,2,4,5}; System.out.println(Arrays.toString(arr)); (2).遍历二维数组 int[][] arr1 = {{1,2,3},{2,3,4},{3,4,5}}; System.out.println(Arrays.deepToString(arr1));