Java打印二维数组

来源:互联网 发布:致远软件 编辑:程序博客网 时间:2024/05/16 13:41

已知打印一维数组的API为System.out.println ( Arrays.toString ();,其参数为数组名或数组指针,其支持的数据类型有很多,如:int[]char[]byte[]等。

但是对二维数组调用同样的API,如:

int[][] test = {{0, 1, 2}, {2, 1}, {1}};        System.out.println ( Arrays.toString (test));

其输出为

[[I@610455d6, [I@511d50c0, [I@60e53b93]

因为上述API只支持一维数组,不支持二维数组,因此应该使用如下的方法:

int[][] test = {{0, 1, 2}, {2, 1}, {1}};        for (int i=0;i<3;i++)            System.out.println ( Arrays.toString (test[i]));

其输出即为所求的:

[0, 1, 2][2, 1][1]
0 0
原创粉丝点击