How to print Two-Dimensional Array like table

后端 未结 15 937
一整个雨季
一整个雨季 2020-11-30 06:55

I\'m having a problem with two dimensional array. I\'m having a display like this:

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 . . . etc

What basi

相关标签:
15条回答
  • 2020-11-30 07:23
    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.
    
    0 讨论(0)
  • 2020-11-30 07:28

    Declared a 7 by 5 array which is similar to yours, with some dummy data. Below should do.

        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 讨论(0)
  • 2020-11-30 07:28
    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");
            }
        }
    }
    

    This works,add a new line in for loop of the row. When the first row will be done printing the code will jump in new line.

    0 讨论(0)
  • 2020-11-30 07:34

    A part from @djechlin answer, you should change the rows and columns. Since you are taken as 7 rows and 5 columns, but actually you want is 7 columns and 5 rows.

    Do this way:-

    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("");
    }
    
    0 讨论(0)
  • 2020-11-30 07:35

    I'll post a solution with a bit more elaboration, in addition to code, as the initial mistake and the subsequent ones that have been demonstrated in comments are common errors in this sort of string concatenation problem.

    From the initial question, as has been adequately explained by @djechlin, we see that there is the need to print a new line after each line of your table has been completed. So, we need this statement:

    System.out.println();
    

    However, printing that immediately after the first print statement gives erroneous results. What gives?

    1 
    2 
    ...
    n 
    

    This is a problem of scope. Notice that there are two loops for a reason -- one loop handles rows, while the other handles columns. Your inner loop, the "j" loop, iterates through each array element "j" for a given "i." Therefore, at the end of the j loop, you should have a single row. You can think of each iterate of this "j" loop as building the "columns" of your table. Since the inner loop builds our columns, we don't want to print our line there -- it would make a new line for each element!

    Once you are out of the j loop, you need to terminate that row before moving on to the next "i" iterate. This is the correct place to handle a new line, because it is the "scope" of your table's rows, instead of your table's columns.

    for(i=0;i<7;i++){
        for(j=0;j<5;j++) {
            System.out.print(twoDm[i][j]+" ");  
        }
        System.out.println();
    }
    

    And you can see that this new line will hold true, even if you change the dimensions of your table by changing the end values of your "i" and "j" loops.

    0 讨论(0)
  • 2020-11-30 07:37

    If you don't mind the commas and the brackets you can simply use:

    System.out.println(Arrays.deepToString(twoDm).replace("], ", "]\n"));
    
    0 讨论(0)
提交回复
热议问题