Java – Print 2D Array

Java example to print 2d array in string format in console or server logs – using Arrays.deepToString() and custom method.

Quick Reference:

int [][] cordinates = { {1,2}, {2,4}, {3,6,9} };

System.out.println( Arrays.deepToString( cordinates ) );    //[[1, 2], [2, 4], [3, 6, 9]]

1. Arrays.deepToString() to print nested arrays

Use deepToString() method to get 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 to convert multi-dimensional arrays to strings.

import java.util.Arrays;
 
public class Print2dArray 
{
    public static void main(String[] args) 
    {
        int [][] cordinates = { {1,2}, {2,4}, {3,6,9} };

        System.out.println( Arrays.deepToString( cordinates ) );
    }
}
[[1, 2], [2, 4], [3, 6, 9]]

2. Custom method to print 2d array (Not recommended)

Use given print2DArray() to print 2d arrays in custom format which may not be possible with default deepToString() method. It uses StringBuilder object to build the string representation of array.

Feel free to customize the method as per your requirements.

	
import java.util.Arrays;

public class TwoDimensionalArrayExamples 
{
	public static void main(String[] args) 
	{
		int [][] cordinates = { {1,2}, {2,4}, {3,6,9} };
		
		System.out.println( print2DArray(cordinates) );
	}
	
	public static String print2DArray(int arr[][])
        {
		StringBuilder builder = new StringBuilder();
		//Open bracket
		builder.append("[");
		
        // Loop through all rows and print
        for (int i = 0; i < arr.length; i++) {
        	builder.append(Arrays.toString(arr[i]) + ", ");
        }
        
        //Delete last two characters
        builder.deleteCharAt(builder.length()-1);
        builder.deleteCharAt(builder.length()-1);
        
        //Close bracket
        builder.append("]");
        
        return builder.toString();
    }
}
[[1, 2], [2, 4], [3, 6, 9]]

It is very simple method and very handy when you want to print 2d arrays in java.

Happy Learning !!

Reference:

Arrays.toString() JavaDoc
Arrays.deepToString() JavaDoc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java – Print 2D Array”

  1. import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) {
            int [][] cordinates = { {1,2}, {2,4}, {3,6,9} };
            System.out.println(Arrays.deepToString(cordinates));
        }
    }
    

    //output
    [[1, 2], [2, 4], [3, 6, 9]]

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.