在 Java 中像表格一样打印二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20960055/
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
Printing a 2d array in Java like a table
提问by Adam Calcanes
I'd like to print an inputed 2-dimensional array like a table i.e if for some reason they put in all 1s...
我想像表格一样打印一个输入的二维数组,即如果由于某种原因他们把所有的 1...
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Just like so above but in the console on Java eclipse, no fancy buttons and GUI's but in the console, here is what I have....
就像上面一样,但是在 Java eclipse 的控制台中,没有花哨的按钮和 GUI,但是在控制台中,这就是我所拥有的....
import java.util.Scanner;
public class Client {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[][] table = new int[4][4];
for (int i=0; i < table.length; i++) {
for (int j=0; j < table.length; j++) {
System.out.println("Enter a number.");
int x = input.nextInt();
table[i][j] = x;
System.out.print(table[i][j] + " ");
}
System.out.println();
}
System.out.println(table);
}
}
And this is what I get when I input everything and the console terminates:
这就是我输入所有内容并且控制台终止时得到的结果:
Enter a number.
1
1 Enter a number.
1
1 Enter a number.
1
1 Enter a number.
1
1
[[I@3fa1732d
采纳答案by tonga
You need to print out the array separately from entering the number. So you can do something like this:
您需要将数组与输入数字分开打印。所以你可以做这样的事情:
public class PrintArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] table = new int[4][4];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table.length; j++) {
// System.out.println("Enter a number.");
int x = input.nextInt();
table[i][j] = x;
}
//System.out.println();
}
// System.out.println(table);
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
}
}
回答by Obicere
Consider using java.util.Arrays
.
考虑使用java.util.Arrays
.
There is a method in there called deepToString
. This will work great here.
里面有一个方法叫做deepToString
. 这将在这里工作得很好。
System.out.println(Arrays.deepToString(table));
Relevant here: Simplest way to print an array in Java
此处相关:用 Java 打印数组的最简单方法
回答by evanchooly
You'll have loop back through those arrays to print out the contents. an array's toString() just prints the reference value.
您将循环遍历这些数组以打印出内容。数组的 toString() 只是打印参考值。
回答by Louis B
System.out.print(table) calls a method in array class that prints out the identifier of the vairable. you need to either create a for loop that will print out each element like System.out.print(table[i][j]) or use the Arrays class and say Arrays.toString(table);
System.out.print(table) 调用数组类中的方法,打印出变量的标识符。您需要创建一个 for 循环来打印每个元素,如 System.out.print(table[i][j]) 或使用 Arrays 类并说 Arrays.toString(table);
回答by 111
Try copying this simple for loop to printing a 4x4 table:
尝试复制这个简单的 for 循环来打印 4x4 表:
Scanner input = new Scanner();
int numArray [] [] = new int [4] [4];
for ( int c = 0; c < 4; c++ ) {
for (int d = 0; d < 4; d++) {
System.out.print("Enter number : ");
nunArray [c][d] = input.nextInt();
}
}
for (int a = 1; a<5;a++) {
for (int b = 1; b <5;b++) {
System.out.print(numArray [a][b]+" ");
}
System.out.println();
}