简体   繁体   English

在Java中显示二维数组

[英]Display a two dimensional array in java

I have an x by y board of numbers stored in a two dimensional array in java that I would like to print out to the user in a nice formatted manner. 我在Java的二维数组中存储着一个x x y的数字板,我希望以一种很好的格式化方式将其输出给用户。 Would there be an easy way to do this in java? 在Java中有一种简单的方法吗?

Here is how I build my board: 这是我构建董事会的方式:

board = new int[TotRow][TotCol];

If you want the number to be padded properly as well use String.format() , 如果您希望正确填充数字,请使用String.format()

for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        System.out.print(String.format("%3d", board[i][j]) + "\t");
    }
    System.out.println();
}

Notice that I have used %3d for formatting, you can use format as required by you 请注意,我已使用%3d进行格式化,您可以根据需要使用format

for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
        System.out.print(board[i][j] + "\t");
    }
    System.out.println();
}

You can print the board two dimensional array like this 您可以像这样打印board two dimensional数组

for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
        System.out.print(board[i][j] + "\t");
    }
    System.out.println();
}

You might prefer the one line, because it's easy, Arrays.deepToString(Object[]) the Javadoc says (in part), 您可能更喜欢一行,因为它很简单,Javadoc说Arrays.deepToString(Object[]) (部分),

This method is designed for converting multidimensional arrays to strings. 此方法设计用于将多维数组转换为字符串。

System.out.println(Arrays.deepToString(board));

Since I don't like relying on tabs, I'd like to mention some approaches that don't rely on them. 由于我不喜欢依赖选项卡,因此我想提及一些不依赖选项卡的方法。

If you know that all of the numbers in the table are nonnegative numbers of, say, 4 digits or less, you can use String.format in a manner similar to Nitin's answer: 如果您知道表中的所有数字都是非负数,例如4位数或更少,则可以使用类似于Nitin的答案的方式使用String.format

for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        System.out.print(String.format("%4d ", board[i][j]));
    }
    System.out.println();
}

The %4d means to format the number using a width of 4 characters, and pad with blanks on the left if the number takes fewer than 4 characters to print. %4d表示使用4个字符的宽度来格式化数字,如果该数字需要少于4个字符来打印,则在其左侧填充空白。 Thus, if there aren't any 5-digit or longer numbers, each print will print exactly 5 characters--a 4-character field for the number, and a space after the number. 因此,如果没有任何5位或更长的数字,则每张print将精确地打印5个字符-该数字为4个字符的字段,并在该数字后加一个空格。 This will make everything line up, as long as none of the numbers is too large. 只要没有一个数字太大,这将使所有内容对齐。 If you do get a number that takes more than 4 characters, the formatting will get messed up. 如果确实收到一个超过4个字符的数字,格式将变得混乱。

Here's a way to adjust the column width so that it's just large enough to hold all the values in the table, and works fine if there are negative numbers: 这是一种调整列宽的方法,以使其足够大以容纳表中的所有值,并且在有负数的情况下可以正常工作:

int maxWidth = 0;
for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        maxWidth = Math.max(maxWidth, Integer.toString(board[i][j]).length());
    }
}
String format = "%" + maxWidth + "d ";
for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        System.out.print(String.format(format, board[i][j]));
    }
    System.out.println();
}

Note: not tested 注意:未测试

You basically just use a nested loop. 您基本上只使用嵌套循环。

for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
        System.out.print(board[i][j] + " ");
    }
    System.out.println();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM