Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    // Gives undesired output:
    System.out.println(Arrays.toString(deepArray));
    // Gives the desired output:
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    
🌐
Baeldung
baeldung.com › home › java › java array › how to print the content of an array in java
How to Print the Content of an Array in Java | Baeldung
September 5, 2024 - Moreover, we use the for loop to ... extra libraries or functionalities. In Java, we can use a for-each or the enhanced for loop to iterate directly over each array element....
🌐
Sentry
sentry.io › sentry answers › java › print an array in java
Print an array in Java | Sentry
We can print simple arrays in Java using the method Arrays.toString(): Click to Copy · Click to Copy · import java.util.Arrays; class Main { public static void main(String[] args) { String[] products = new String[]{"Coffee", "Tea", "Chocolate ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-write-an-array-of-strings-to-the-output-console
Java Program to Write an Array of Strings to the Output Console - GeeksforGeeks
November 1, 2023 - In this method, we will access each element of the array and would write it to the output console. Java · import java.io.*; class GFG { public static void main(String[] args) { String gfg[] = new String[3]; gfg[0] = "Geeks"; gfg[1] = "for"; gfg[2] = "Geeks"; for (int i = 0; i <= 2; i++) { System.out.print(gfg[i] + " "); } } } Output ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › simplest-method-to-print-array-in-java
Simplest Method to Print Array in Java - GeeksforGeeks
July 23, 2025 - // Java Program to print the elements // using Arrays.toString() Method import java.util.Arrays; public class GFG { public static void main(String[] args) { // integer array int[] a = { 1, 2, 3, 4, 5 }; // string array String[] s = { "vivek", ...
🌐
UpStack
upstackhq.com › upstack blog › software development
How To Print An Array In Java | Upstack
Example of a Java program that uses the Arrays.toString() method to print an array: // import Arrays class import java.util.Arrays; public class DisplayArray { public static void main(String[] args) { // declare and initialize an array int arr[] ...
🌐
Vultr Docs
docs.vultr.com › java › examples › print-an-array
Java Program to Print an Array | Vultr Docs
November 26, 2024 - This guide on Java printing arrays will help you understand efficient techniques for handling array output. Define an array of integers. Use a for-loop to iterate through the array and print each element.
🌐
Medium
medium.com › @pratiyush1 › how-to-print-the-content-of-an-array-in-java-eea946f2945c
How to Print the Content of an Array in Java | by Pratiyush | Medium
October 15, 2024 - For example, use for loops for fine-grained control, Arrays.toString() for quick debugging of simple arrays, Arrays.deepToString() for printing nested arrays, streams for functional processing, and String.join() for easily formatted string arrays. Understanding these options will help you print arrays effectively and efficiently in Java.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java collections › how to print an array in java
How to Print an Array in Java
Here is a list of ways to print arrays in Java that we will be exploring in this article. ... This is the simplest way, to begin with. Here’s how you can do it. public class printArrayMethod1 { public static void main(String[] args) { String[] monthsOfTheYear = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; System.out.println("Months of the year are as follows:"); // Method I - Printing array using for loop for (int i = 0; i < monthsOfTheYear.length; i++) { System.out.println(monthsOfTheYear[i]); } } }
Published   February 14, 2025
🌐
Javatpoint
javatpoint.com › how-to-print-array-in-java
Print Array in Java
How to print array in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
🌐
YouTube
youtube.com › watch
How to Print a String Array Using Java - YouTube
How to Print a String Array Using JavaGreetings, today we shall be looking at multiple ways you can print a string array using Java.Our first method of print...
Published   December 6, 2022
🌐
Mkyong
mkyong.com › home › java › how to print an array in java
How to print an Array in Java - Mkyong.com
October 18, 2021 - package com.mkyong; import java.util.Arrays; public class PrintArray1 { public static void main(String[] args) { // string array String[] strArray = new String[]{"Java", "Node", "Python", "Ruby"}; System.out.println(Arrays.toString(strArray)); // Output : [Java, Node, Python, Ruby] // int Array int[] intArray = {1, 3, 5, 7, 9}; System.out.println(Arrays.toString(intArray)); // Output : [1, 3, 5, 7, 9] // 2d array, need Arrays.deepToString String[][] strArrayDeep = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}}; System.out.println(Arrays.toString(strArrayDeep)); // Output : [[Lj
🌐
Programiz
programiz.com › java-programming › examples › print-array
Java Program to Print an Array
It accesses each element in the array and prints using println(). import java.util.Arrays; public class Array { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(array)); } }
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array – how to print elements of an array in java?
Java Array - How To Print Elements Of An Array In Java
April 1, 2025 - Following is the program that ... are:"); for(int i =0; i&lt;5;i++) //iterate through every array element System.out.print(myArray[i] + " "); //print the array element } }...
🌐
iO Flood
ioflood.com › blog › printing-an-array-in-java
Printing an Array in Java: A Guide For Printing to Screen
February 27, 2024 - In this example, we’ve created an integer array named ‘array’ with elements 1, 2, and 3. We then use the Arrays.toString() method to convert this array into a string format that can be printed.
🌐
Java67
java67.com › 2014 › 03 › how-to-print-array-in-java-example-tutorial.html
How to Print Array with elements in Java? [Solution + Example] | Java67
String random = "In Java programming langue, array is object"; byte[] bytes = random.getBytes(); System.out.println("What is inside bytes : " + bytes); System.out.println("Not visible, check closely .." + Arrays.toString(bytes)); Output What is inside bytes : [B@31602bbc Not visible, check closely ..[73, 110, 32, 74, 97, 118, 97, 32, 112, 114, 111, 103, 114, 97, 109, 109, 105, 110, 103, 32, 108, 97, 110, 97, 103, 117, 101, 44, 32, 97, 114, 114, 97, 121, 32, 105, 115, 32, 111, 98, 106, 101, 99, 116]
🌐
Reddit
reddit.com › r/learnjava › how to print an array of list of arrays
r/learnjava on Reddit: How to print an array of List of arrays
December 22, 2023 -

List<int[]>[] adj = new ArrayList[n + 1];

I tried this

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

and it prints [[[I@20ad9418], [[I@31cefde0], [[I@439f5b3d], [], []]

- This is how I am expecting the result to be [ [], [], [[1, 1], [3, 1]], [[4, 1]], []]

- It's okay if the format isn't as it is but the below would also work

idx 0: []
idx 1: [] 
idx 2: [1, 1], [3, 1] 
idx 3: [4, 1]
idx 4: []

Edit:

Thank you all, for your wisdom words, I am able to print using both nested for loops and streams. And I am commenting so that it will be helpful for future learners :)

// Print array of lists of arrays Using regular for loops

for (int i = 0; i < adj.length; i++) {
    System.out.print("idx " + i + ": ");
    for (int j = 0; j < adj[i].size(); j++) {
        System.out.print(Arrays.toString(adj[i].get(j)));
    }
    System.out.println();
}

// Print array of lists of arrays Using Stream

Arrays.stream(adj).forEach(lst -> { 
    System.out.println(Arrays.deepToString(lst.toArray())); 
});
🌐
freeCodeCamp
freecodecamp.org › news › java-array-methods-how-to-print-an-array-in-java
Java Array Methods – How to Print an Array in Java
July 20, 2020 - We can not print arrays in Java using a plain System.out.println() method. Instead, these are the following ways we can print an array: ... Let’s see them one by one. ... int[] intArray = {2,5,46,12,34}; for(int i=0; i<intArray.length; i++){ System.out.print(intArray[i]); // output: 25461234 } All wrapper classes override Object.toString() and return a string representation of their value.
🌐
Scaler
scaler.com › topics › print-array-in-java
How to Print Array in Java - Scaler Topics
July 27, 2022 - This Java program initializes an array of strings and utilizes the Stream API to iterate through the elements. It invokes the forEach() method to print each element of the array to the console using method reference syntax.