How to print Two-Dimensional Array like table

后端 未结 15 935
一整个雨季
一整个雨季 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:16

    This might be late however this method does what you ask in a perfect manner, it even shows the elements in ' table - like ' style, which is brilliant for beginners to really understand how an Multidimensional Array looks.

    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 
            }
        }
    

    After you complete creating this method, you simply put this into your main method:

    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)
    

    NOTE. Since we made the method so that it requires Multidimensional Array as a input it wont work for 1 dimensional arrays (which would make no sense anyways)

    Source: enter link description here

    PS. It might be confusing a little bit since I used my language to name the elements / variables, however CBA to translate them, sorry.

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

    You could write a method to print a 2d array like this:

    //Displays a 2d array in the console, one line per row.
    static void printMatrix(int[][] grid) {
        for(int r=0; r<grid.length; r++) {
           for(int c=0; c<grid[r].length; c++)
               System.out.print(grid[r][c] + " ");
           System.out.println();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 07:18

    ALL OF YOU PLEASE LOOT AT IT I Am amazed it need little IQ just get length by arr[0].length and problem solved

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

    Iliya,

    Sorry for that.

    you code is work. but its had some problem with Array row and columns

    here i correct your code this work correctly, you can try this ..

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

    and out put look like

    ----------------------
    |    1 |    2 |    5 |
    ----------------------
    |    3 |    4 |    6 |
    ----------------------
    |    7 |    8 |    9 |
    ----------------------
    
    0 讨论(0)
  • 2020-11-30 07:22

    Just for the records, Java 8 provides a better alternative.

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

    Output

    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  
    

    Of course, you might swap the 7 & 5 as mentioned in other answers, to get 7 per row.

    0 讨论(0)
提交回复
热议问题