Java中打印数组时不想用for遍历数组,可以试试下面几种方法
ArrayList直接打印
1 | ArrayList<Integer> list = new ArrayList<>(); |
输出1
[1, 2, 3, 4]
Arrays类打印数组
java.util.Arrays的toString()方法1
System.out.println(Arrays.toString(new int[] {1, 2, 3, 4}));
输出1
[1, 2, 3, 4]
Arrays类打印二维数组
java.util.Arrays的deeptoString()方法1
System.out.println(Arrays.deepToString(new int[][] {{1, 2}, {3, 4}}));
输出1
[[1, 2], [3, 4]]