HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

Java Print 2D Array – Arrays.deepToString()

By Lokesh Gupta | Filed Under: Array

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

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()

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 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]]

2) Custom Method

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

//Output:

[[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

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Feedback, Discussion and Comments

  1. kon

    July 19, 2018

    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

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Help me fight spammers. Solve this simple math. *

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Java Array

  • Java – Array Introduction
  • Java – Print Array
  • Java – Print 2D Array
  • Java – Copy Array
  • Java – Copy Array Range
  • Java – Clone Array
  • Java – Array Deep Copy
  • Java – String to String[]
  • Java – byte[] to String
  • Java – String to byte[]
  • Java – Array Union
  • Java – Array Intersection
  • Array – Remove duplicate elements

Java Collections

  • Collections Framework
  • Array
  • ArrayList
  • LinkedList
  • HashMap
  • Hashtable
  • LinkedHashMap
  • TreeMap
  • HashSet
  • LinkedHashSet
  • TreeSet
  • Comparable
  • Comparator
  • Iterator
  • ListIterator
  • Spliterator
  • PriorityQueue
  • PriorityBlockingQueue
  • ArrayBlockingQueue
  • LinkedTransferQueue
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet
  • Collection Sorting
  • Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap