Categories
arrays java printing

What’s the simplest way to print a Java array?

2274

In Java, arrays don’t override toString(), so if you try to print one directly, you get the className + ‘@’ + the hex of the hashCode of the array, as defined by Object.toString():

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray);     // prints something like '[[email protected]'

But usually, we’d actually want something more like [1, 2, 3, 4, 5]. What’s the simplest way of doing that? Here are some example inputs and outputs:

// Array of primitives:
int[] intArray = new int[] {1, 2, 3, 4, 5};
//output: [1, 2, 3, 4, 5]

// Array of object references:
String[] strArray = new String[] {"John", "Mary", "Bob"};
//output: [John, Mary, Bob]

7

  • 9

    What do you want the representation to be for objects other than strings? The result of calling toString? In quotes or not?

    – Jon Skeet

    Jan 3, 2009 at 20:41

  • 3

    Yes objects would be represented by their toString() method and without quotes (just edited the example output).

    Jan 3, 2009 at 20:42

  • 2

    In practice, closely related to stackoverflow.com/questions/29140402/…

    – Raedwald

    Jan 24, 2016 at 18:02

  • 1

    That weird output is not necessarily the memory location. It’s the hashCode() in hexadecimal. See Object#toString().

    – 4castle

    Nov 10, 2016 at 21:22

  • 1

    To print single dimensional or multi-dimensional array in java8 check stackoverflow.com/questions/409784/…

    Nov 16, 2016 at 1:28


3046

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"}};
    System.out.println(Arrays.toString(deepArray));
    //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

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

8

  • 4

    What if we have an array of strings, and want simple output; like: String[] array = {"John", "Mahta", "Sara"}, and we want this output without bracket and commas: John Mahta Sara?

    – Hengameh

    Aug 29, 2015 at 2:34

  • 3

    @Hengameh: There are several other ways to do this, but my favorite is this one: javahotchocolate.com/notes/java.html#arrays-tostring .

    Aug 29, 2015 at 6:16

  • 5

    FYI, Arrays.deepToString() accepts only an Object [] (or an array of classes that extend Object, such as Integer, so it won’t work on a primitive array of type int []. But Arrays.toString(<int array>) works fine for primitive arrays.

    – Marcus

    Dec 11, 2015 at 23:25


  • 6

    @Hengameh There’s a method dedicated to that. System.out.println(String.join(" ", new String[]{"John", "Mahta", "Sara"})) will print John Mahta Sara.

    Mar 13, 2017 at 14:15

  • 9

    Don’t forget “import java.util.Arrays;”

    Oct 21, 2019 at 21:48

406

Always check the standard libraries first.

import java.util.Arrays;

Then try:

System.out.println(Arrays.toString(array));

or if your array contains other arrays as elements:

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

1

  • 22

    @Hengameh Nowadays with Java 8: String.join(" ", Arrays.asList(array)). doc

    – Justin

    Mar 4, 2016 at 23:14


110

This is nice to know, however, as for “always check the standard libraries first” I’d never have stumbled upon the trick of Arrays.toString( myarray )

–since I was concentrating on the type of myarray to see how to do this. I didn’t want to have to iterate through the thing: I wanted an easy call to make it come out similar to what I see in the Eclipse debugger and myarray.toString() just wasn’t doing it.

import java.util.Arrays;
.
.
.
System.out.println( Arrays.toString( myarray ) );

0