Java中数组的toString()方法调用的是Object.toString()打印类型的classname与哈希值.
Arrays.toString只能打印一维数组
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(intArray));
// output: [1, 2, 3, 4, 5]
String[] nameArray = new String[] {"a", "b", "c", "d"};
System.out.println(Arrays.toString(nameArray));
// output: [a, b, c, d]
int[][] intNestedArray = new int[][]{{1, 2}, {3, 4}, {5,6}};
System.out.println(Arrays.deepToString(intNestedArray));
// output: [[1, 2], [3, 4], [5,6]]
Java8以后的新增方法
int[] intArray = new int[]{1, 2, 3, 4, 5};
Arrays.stream(intArray).forEach(System.out::println);
// joining(delimiter) 生成字符串来输出。
String[] nameArray = new String[]{"a", "b", "c", "d"};
String res = Arrays.stream(nameArray).collect(joining(","));
System.out.println(res);
// output: a,b,c,d
int[] arr = new int[]{0,1,2,3,4,5,6,7,8,9};
// 普通for循环打印
System.out.println("普通for循环打印:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "、");
}
// 增强for循环打印
System.out.println("增强for循环打印:");
for (int num : arr) {
System.out.print(num + "、");
}