HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Array / Java – Print 2D Array

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

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

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

Comments are closed on this article!

Search Tutorials

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 Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

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

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)