Simplest Way to Print a Java Array

What’s the Simplest Way to Print a Java Array?

In Java, arrays don’t override the toString() method. Therefore, printing an array directly results in the output of the class name followed by ‘@’ and the hexadecimal hash code of the array, as defined by Object.toString(). For example:

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // Prints something like '[I@3343c8b3'

However, we usually want a more readable output like [1, 2, 3, 4, 5]. What’s the simplest way to achieve 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]
    

Can anyone help ?

I’ve been working with Java for a good number of years now, and one of the simplest ways I’ve found to print arrays is by using Arrays.toString() and Arrays.deepToString(). Since Java 5, you can use Arrays.toString(arr) for simple arrays or Arrays.deepToString(arr) for nested arrays. The Object[] version invokes toString() on each object within the array. The output is formatted exactly as you need.

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]

Integer Array:


int[] intArray = {7, 9, 5, 1, 3};

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

Output:


[7, 9, 5, 1, 3]

These methods provide a straightforward way to print arrays with the desired formatting.

With my experience in Java development, I’ve found that sometimes the simplest solutions are the best. Using a regular for loop is a very basic and effective way to print an array. Here’s a sample code based on your intArray:


for (int i = 0; i < intArray.length; i++) {

System.out.print(intArray[i] + ", ");

}

This will give you the desired output:


1, 2, 3, 4, 5,

It’s a bit more verbose, but it gives you full control over the formatting.

Drawing from my years of coding in Java, another approach to consider is using Arrays.asList(). This method works well if the array contains objects. For arrays with primitive types, you need to use wrapper classes instead:

Example:

int[] a = new int[]{1, 2, 3, 4, 5};

Replace it with:

Integer[] a = new Integer[]{1, 2, 3, 4, 5};

Update:

However, converting a primitive array to an object array or using an object array can be costly and may slow down execution due to Java’s autoboxing feature. Therefore, for printing purposes, this method is not recommended. Instead, you can create a function that takes an array as a parameter and prints it in the desired format:

public void printArray(int[] a) {
    for (int i = 0; i < a.length; i++) {
        System.out.print(a[i]);
        if (i < a.length - 1) System.out.print(", ");
    }
    System.out.println();
}

This approach avoids the overhead associated with autoboxing and provides a more efficient solution for printing arrays.