Java - Best way to print 2D array?

前端 未结 12 622
庸人自扰
庸人自扰 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:41

    @Ashika's answer works fantastically if you want (0,0) to be represented in the top, left corner, per normal CS convention. If however you would prefer to use normal mathematical convention and put (0,0) in the lower left hand corner, you could use this:

    LinkedList printList = new LinkedList();
    for (char[] row: array) {
        printList.addFirst(Arrays.toString(row));;
    }
    while (!printList.isEmpty())
        System.out.println(printList.removeFirst());
    

    This used LIFO (Last In First Out) to reverse the order at print time.

提交回复
热议问题