Java - 打印二维数组的最佳方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19648240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Best way to print 2D array?
提问by Chip Goon Lewin
I was wondering what the best way of printing a 2D array was. This is some code that I have and I was just wondering if this is good practice or not. Also correct me in any other mistakes I made in this code if you find any. Thanks!
我想知道打印二维数组的最佳方式是什么。这是我拥有的一些代码,我只是想知道这是否是好的做法。如果您发现任何其他错误,还请纠正我在此代码中犯的任何其他错误。谢谢!
int rows = 5;
int columns = 3;
int[][] array = new int[rows][columns];
for(int i = 0; i<rows; i++)
for(int j = 0; j<columns; j++)
array[i][j] = 0;
for(int i = 0; i<rows; i++)
{
for(int j = 0; j<columns; j++)
{
System.out.print(array[i][j]);
}
System.out.println();
}
回答by Prabhakaran Ramaswamy
You can print in simple way.
您可以以简单的方式打印。
Use below to print 2D array
使用下面打印二维数组
int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));
Use below to print 1D array
使用下面打印一维数组
int[] array = new int[size];
System.out.println(Arrays.toString(array));
回答by ashika
There is nothing wrong with what you have. Double-nested for loops should be easily digested by anyone reading your code.
你所拥有的并没有错。任何阅读您代码的人都应该很容易理解双嵌套 for 循环。
That said, the following formulation is denser and more idiomatic java. I'd suggest poking around some of the static utility classes like Arraysand Collectionssooner than later. Tons of boilerplate can be shaved off by their efficient use.
也就是说,以下公式更密集,更惯用 java. 我建议尽早探索一些静态实用程序类,如数组和集合。大量的样板可以通过它们的有效使用来削减。
for (int[] row : array)
{
Arrays.fill(row, 0);
System.out.println(Arrays.toString(row));
}
回答by Max von Hippel
@Ashika's answer works fantastically if you want (0,0) to be represented in the top, left corner, per normal CS convention. If however you would prefer to use normal mathematical convention and put (0,0) in the lower left hand corner, you could use this:
如果您希望 (0,0) 按照正常的 CS 惯例在左上角表示,@Ashika 的答案非常有效。但是,如果您更喜欢使用正常的数学约定并将 (0,0) 放在左下角,则可以使用以下命令:
LinkedList<String> printList = new LinkedList<String>();
for (char[] row: array) {
printList.addFirst(Arrays.toString(row));;
}
while (!printList.isEmpty())
System.out.println(printList.removeFirst());
This used LIFO (Last In First Out) to reverse the order at print time.
这使用 LIFO(后进先出)在打印时反转顺序。
回答by snr
I would prefer generally foreach
when I don't need making arithmetic operations with their indices.
foreach
当我不需要对其索引进行算术运算时,我通常更喜欢。
for (int[] x : array)
{
for (int y : x)
{
System.out.print(y + " ");
}
System.out.println();
}
回答by Plcode
That's the best I guess:
这是我认为最好的:
for (int[] row : matrix){
System.out.println(Arrays.toString(row));
}
回答by Baseem
|1 2 3|
|4 5 6|
Use the code below to print the values.
使用下面的代码打印值。
System.out.println(Arrays.deepToString());
Output will look like this (the whole matrix in one line):
输出将如下所示(整个矩阵在一行中):
[[1,2,3],[4,5,6]]
回答by matthewpliddy
Simple and clean way to print a 2D array.
打印二维数组的简单而干净的方法。
System.out.println(Arrays.deepToString(array).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));
回答by Nambi_0915
Try this,
尝试这个,
for (char[] temp : box) {
System.err.println(Arrays.toString(temp).replaceAll(",", " ").replaceAll("\[|\]", ""));
}
回答by suvojit_007
Two-liner with new line:
新线两线:
for(int[] x: matrix)
System.out.println(Arrays.toString(x));
One liner without new line:
没有新线的一个班轮:
System.out.println(Arrays.deepToString(matrix));
回答by ZhaoGang
From Oracle Offical Java 8 Doc:
public static String deepToString(Object[] a)
public static String deepToString(Object[] a)
Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
返回指定数组的“深层内容”的字符串表示形式。如果数组包含其他数组作为元素,则字符串表示包含它们的内容等等。此方法旨在将多维数组转换为字符串。