Java - Best way to print 2D array?

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 other mistakes I made in this code if you find any. Thanks!

int rows = 5;
int columns = 3;

int[][] array = new int[rows][columns];

for(int i = 0; i<rows; i++)
    for(int j = 0; j<columns; j++)
        array[i][j] = 0;

for(int i = 0; i<rows; i++)
{
    for(int j = 0; j<columns; j++)
    {
        System.out.print(array[i][j]);
    }
    System.out.println();
}

Answers


You can print in simple way.

Use below to print 2D array

int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));

Use below to print 1D array

int[] array = new int[size];
System.out.println(Arrays.toString(array));

I would prefer generally foreach when I don't need making arithmetic operations with their indices.

for (int[] x : array)
{
   for (int y : x)
   {
        System.out.print(y + " ");
   }
   System.out.println();
}

Simple and clean way to print a 2D array.

System.out.println(Arrays.deepToString(array).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

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

That's the best I guess:

   for (int[] row : matrix){
    System.out.println(Arrays.toString(row));
   }

@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<String> printList = new LinkedList<String>();
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.


|1 2 3|
|4 5 6| 

Use the code below to print the values.

System.out.println(Arrays.deepToString());

Output will look like this (the whole matrix in one line):

[[1,2,3],[4,5,6]]

Two-liner with new line:

for(int[] x: matrix)
            System.out.println(Arrays.toString(x));

One liner without new line:

System.out.println(Arrays.deepToString(matrix));

Try this,

for (char[] temp : box) {
    System.err.println(Arrays.toString(temp).replaceAll(",", " ").replaceAll("\\[|\\]", ""));
}

From Oricle Offical Java 8 Doc:

public static String deepToString(Object[] a)

Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.


Need Your Help

SQL query for dynamic table column selection from checkbox in codeigniter?

sql codeigniter dynamic checkbox

I am trying to retrieve the columns from the database based on checkbox selection in codeigniter. But I have no idea how to do it?

Dynamically change display orders of data in ColdFusion

coldfusion treeview

Using ColdFusion, I'm trying to setup a menu and menu items application where you can change the order of the menu and also the menu items within the menu. I've found something relative, but it's not