Мне было интересно, какой лучший способ печати 2D-массива на Java?
Мне просто интересно, является ли этот код хорошей практикой или нет? Также любые другие ошибки, которые я допустил в этом коде, если вы их обнаружите.
introws=5; intcolumns=3;
int[][] array = newint[rows][columns];
for (inti=0; i<rows; i++) for (intj=0; j<columns; j++) array[i][j] = 0;
for (inti=0; i<rows; i++) { for (intj=0; j<columns; j++) { System.out.print(array[i][j]); } System.out.println(); }
Переведено автоматически
Ответ 1
Вы можете печатать простым способом.
Используйте приведенное ниже для печати 2D-массива
There is nothing wrong with what you have. Double-nested for loops should be easily digested by anyone reading your code.
That said, the following formulation is denser and more idiomatic java. I'd suggest poking around some of the static utility classes like Arrays and Collections sooner than later. Tons of boilerplate can be shaved off by their efficient use.
for (int[] row : array) { Arrays.fill(row, 0); System.out.println(Arrays.toString(row)); }