如何打印像表一样的二维数组
我有二维数组的问题。 我有这样的显示:
1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 . . . etc
基本上我想要的是显示它显示为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ... etc
这是我的代码:
int twoDm[][]= new int[7][5]; int i,j,k=1; for(i=0;i<7;i++){ for(j=0;j<5;j++) { twoDm[i][j]=k; k++;} } for(i=0;i<7;i++){ for(j=0;j<5;j++) { System.out.print(twoDm[i][j]+" "); System.out.print("");} }
public class FormattedTablePrint { public static void printRow(int[] row) { for (int i : row) { System.out.print(i); System.out.print("\t"); } System.out.println(); } public static void main(String[] args) { int twoDm[][]= new int[7][5]; int i,j,k=1; for(i=0;i<7;i++) { for(j=0;j<5;j++) { twoDm[i][j]=k; k++; } } for(int[] row : twoDm) { printRow(row); } } }
产量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
当然,你可以像其他答案中提到的那样交换7和5,每行7个。
你需要在每一行之后打印一个新行…… System.out.print("\n")
,或者使用println
等。就目前而言,你只是打印什么 – System.out.print("")
,用println
替换print
或用"\n"
替换"\n"
。
您可以编写一个方法来打印像这样的二维数组:
//Displays a 2d array in the console, one line per row. static void printMatrix(int[][] grid) { for(int r=0; r
如果你不介意逗号和括号,你可以简单地使用:
System.out.println(Arrays.deepToString(twoDm).replace("], ", "]\n")));
来自@djechlin的部分回答,您应该更改行和列。 由于您被视为7行和5列,但实际上您需要的是7列和5行。
这样做: –
int twoDm[][]= new int[5][7]; for(i=0;i<5;i++){ for(j=0;j<7;j++) { System.out.print(twoDm[i][j]+" "); } System.out.println(""); }
除了代码之外,我将发布一个更详细的解决方案,因为最初的错误以及随后在注释中演示的错误是这种字符串连接问题中的常见错误。
从最初的问题开始,正如@djechlin已经充分解释的那样,我们发现在表格的每一行完成之后需要打印一个新行。 所以,我们需要这样的声明:
System.out.println();
但是,在第一个打印语句之后立即打印会产生错误结果。 是什么赋予了?
1 2 ... n
这是范围问题。 请注意,有两个循环是有原因的 – 一个循环处理行,而另一个循环处理列。 对于给定的“i”,你的内部循环“j”循环遍历每个数组元素“j”。 因此,在j循环结束时,您应该有一行。 您可以将此“j”循环的每个迭代视为构建表的“列”。 由于内循环构建了我们的列,我们不想在那里打印我们的行 – 它会为每个元素创建一个新行!
一旦你离开j循环,你需要终止该行,然后继续下一个“i”迭代。 这是处理新行的正确位置,因为它是表行的“范围”,而不是表的列。
for(i=0;i<7;i++){ for(j=0;j<5;j++) { System.out.print(twoDm[i][j]+" "); } System.out.println(); }
即使您通过更改“i”和“j”循环的结束值来更改表的尺寸,您也可以看到此新行将成立。
仅仅为了记录,Java 8提供了更好的选择。
int[][] table = new int[][]{{2,4,5},{6,34,7},{23,57,2}}; System.out.println(Stream.of(table) .map(rowParts -> Stream.of(rowParts .map(element -> ((Integer)element).toString()) .collect(Collectors.joining("\t"))) .collect(Collectors.joining("\n")));
以格式化方式打印2Darrays的更高效和简便方法:
尝试这个:
public static void print(int[][] puzzle) { for (int[] row : puzzle) { for (int elem : row) { System.out.printf("%4d", elem); } System.out.println(); } System.out.println(); }
样本输出:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
您可以创建一个将矩阵打印为表格的方法:
注意:对于具有多个数字和非方形矩阵的数字的矩阵,这不起作用。
public static void printMatrix(int size,int row,int[][] matrix){ for(int i = 0;i < 7 * size ;i++){ System.out.print("-"); } System.out.println("-"); for(int i = 1;i <= matrix[row].length;i++){ System.out.printf("| %4d ",matrix[row][i - 1]); } System.out.println("|"); if(row == size - 1){ // when we reach the last row, // print bottom line "---------" for(int i = 0;i < 7 * size ;i++){ System.out.print("-"); } System.out.println("-"); } } public static void main(String[] args){ int[][] matrix = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; // print the elements of each row: int rowsLength = matrix.length; for(int k = 0; k < rowsLength; k++){ printMatrix(rowsLength,k,matrix); } }
输出:
--------------------- | 1 | 2 | 3 | 4 | --------------------- | 5 | 6 | 7 | 8 | --------------------- | 9 | 10 | 11 | 12 | --------------------- | 13 | 14 | 15 | 16 | ---------------------
我在练习循环和数组时创建了这个方法,我宁愿使用:
System.out.println(Arrays.deepToString(matrix).replace("], ", "]\n")));
Iliya,
对不起。
你的代码就是工作。 但它的Array行和列有一些问题
在这里我纠正你的代码这项工作正确,你可以试试这个..
public static void printMatrix(int size, int row, int[][] matrix) { for (int i = 0; i < 7 * matrix[row].length; i++) { System.out.print("-"); } System.out.println("-"); for (int i = 1; i <= matrix[row].length; i++) { System.out.printf("| %4d ", matrix[row][i - 1]); } System.out.println("|"); if (row == size - 1) { // when we reach the last row, // print bottom line "---------" for (int i = 0; i < 7 * matrix[row].length; i++) { System.out.print("-"); } System.out.println("-"); } } public static void length(int[][] matrix) { int rowsLength = matrix.length; for (int k = 0; k < rowsLength; k++) { printMatrix(rowsLength, k, matrix); } } public static void main(String[] args) { int[][] matrix = { { 1, 2, 5 }, { 3, 4, 6 }, { 7, 8, 9 } }; length(matrix); }
出来看起来像
---------------------- | 1 | 2 | 5 | ---------------------- | 3 | 4 | 6 | ---------------------- | 7 | 8 | 9 | ----------------------
这可能是迟到的,但是这个方法以完美的方式完成你所要求的,它甚至以“表格式”的方式显示元素,这对初学者来说非常棒,可以真正理解多维数组的外观。
public static void display(int x[][]) // So we allow the method to take as input Multidimensional arrays { //Here we use 2 loops, the first one is for the rows and the second one inside of the rows is for the columns for(int rreshti = 0; rreshti < x.length; rreshti++) // Loop for the rows { for(int kolona = 0; kolona < x[rreshti].length;kolona++) // Loop for the columns { System.out.print(x[rreshti][kolona] + "\t"); // the \t simply spaces out the elements for a clear view } System.out.println(); // And this empty outputprint, simply makes sure each row (the groups we wrote in the beggining in seperate {}), is written in a new line, to make it much clear and give it a table-like look } }
完成创建此方法后,只需将其放入main方法:
display(*arrayName*); // So we call the method by its name, which can be anything, does not matter, and give that method an input (the Array's name)
注意。 因为我们制作了这个方法,所以它需要多维数组作为输入,所以它不适用于1维数组(这无论如何都没有意义)
来源: 在此处输入链接说明
PS。 由于我使用我的语言来命名元素/变量,但是可能会让人感到困惑,但CBA要翻译它们,对不起。
For traversing through a 2D array, I think the following for loop can be used. for(int a[]: twoDm) { System.out.println(Arrays.toString(a)); } if you don't want the commas you can string replace if you want this to be performant you should loop through a[] and then print it.
public static void main(String[] args) { int[][] matrix = { { 1, 2, 5 }, { 3, 4, 6 }, { 7, 8, 9 } }; System.out.println(" ** Matrix ** "); for (int rows = 0; rows < 3; rows++) { System.out.println("\n"); for (int columns = 0; columns < matrix[rows].length; columns++) { System.out.print(matrix[rows][columns] + "\t"); } } }
这样可行,在行的for循环中添加一个新行。 当第一行将完成打印时,代码将跳转到新行。
你要满意的一切我很惊讶它需要一点智商只需通过arr [0]得到长度。长度和问题解决了
for (int i = 0; i < test.length; i++) { for (int j = 0; j < test[0].length; j++) { System.out.print(test[i][j]); } System.out.println(); }