如何像表格一样打印二维数组

17

我遇到了一个二维数组的问题。我的显示结果是这样的:

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("");}
        }
16个回答

1

声明了一个7行5列的数组,与你的类似,其中包含一些虚拟数据。下面应该可以实现。

    int[][] array = {{21, 12, 32, 14, 52}, {43, 43, 55, 66, 72}, {57, 64, 52, 57, 88},{52, 33, 54, 37, 82},{55, 62, 35, 17, 28},{55, 66, 58, 72, 28}}; 
    
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array[i].length; j++) {
            System.out.print(array[i][j] + " ");
        }
        System.out.println(); // the trick is here, print a new line after iterating first row.
    }

0
public class class1 {
    public static void main(String[] args){
        int[][] a={{1, 2, 3}, {4, 5, 6}};
        
        for (int i = 0; i< a.length; i++){
            System.out.print("Row" + (i + 1) + ": ");
            for (int j = 0; j < a[i].length; j++){
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}

结果:

Row1: 1 2 3                                                                                          
Row2: 4 5 6

-1
这可能有点晚了,但是这种方法完美地实现了你的要求,甚至以“表格”样式显示元素,对于初学者来说非常棒,可以真正理解多维数组的外观。
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 
        }
    }

完成创建该方法后,只需将其放入主方法中即可:
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)

注意。由于我们设计的方法需要多维数组作为输入,因此它不适用于一维数组(这也毫无意义)。

来源:在此输入链接描述

另外,可能会有点混淆,因为我使用了我的语言来命名元素/变量,但是我懒得翻译,抱歉。


-2
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循环的行中添加一个新行。当第一行打印完成后,代码会跳到新的一行。


这本质上就是 Andrew Thompson 的回答所做的事情。 - Uyghur Lives Matter

-2
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.

1
请尝试在回答中提供上下文。独立的答案可能会解决特定情况下的问题,但是解释可能会解决许多未来的问题。请尽量包含您认为导致问题的原因以及为什么您的答案可以解决它。 - Newd

-3

大家请看,我很惊讶这只需要一点智商,通过 arr[0].length 获取长度就可以解决问题了。

   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();
    }

1
这个答案真的很冗余。因为它在每一行数字之间没有添加空格,所以基本上是现有答案的更差版本。 - Uyghur Lives Matter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接