printing out a 2-D array in Matrix format

后端 未结 9 1425
暖寄归人
暖寄归人 2020-12-08 03:13

How can I print out a simple int [][] in the matrix box format like the format in which we handwrite matrices in. A simple run of loops doesn\'t apparently work. If it helps

9条回答
  •  温柔的废话
    2020-12-08 03:59

    int[][] matrix = {
        {1,2,3},
        {4,5,6},
        {7,8,9},
        {10,11,12}
    };
    
    printMatrix(matrix);
    
    public void printMatrix(int[][] m){
        try{
            int rows = m.length;
            int columns = m[0].length;
            String str = "|\t";
    
            for(int i=0;i

    Output:

    |   1   2   3   |
    |   4   5   6   |
    |   7   8   9   |
    |   10  11  12  |
    

提交回复
热议问题