How to print Two-Dimensional Array like table

后端 未结 15 1119
一整个雨季
一整个雨季 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: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.
        }
    

提交回复
热议问题