Java - Best way to print 2D array?

前端 未结 12 820
庸人自扰
庸人自扰 2020-11-22 15:07

I was wondering what the best way of printing a 2D array was. This is some code that I have and I was just wondering if this is good practice or not. Also correct me in any

12条回答
  •  孤独总比滥情好
    2020-11-22 15:42

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

提交回复
热议问题