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

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 '[I@3343c8b3'

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]

Method 1: Using Arrays.toString()

The simplest way to print a Java array is by using the Arrays.toString() method. This method is part of the java.util.Arrays class and is designed specifically for printing arrays in a readable format.

import java.util.Arrays;

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(intArray));
// Output: [1, 2, 3, 4, 5]

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

By using the Arrays.toString() method, you can easily print both primitive arrays and arrays of object references in a formatted way. It automatically transforms the array into a string representation with the desired output.

It's important to note that if the array contains nested arrays or complex objects, the toString() method of the nested objects will be called recursively, resulting in a multi-level representation of the array.

Method 2: Using a Loop

If you prefer a more manual approach or are working with older versions of Java that don't have the Arrays.toString() method, you can use a loop to iterate through the array elements and print them individually.

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.print("[");
for (int i = 0; i < intArray.length; i++) {
    System.out.print(intArray[i]);
    if (i < intArray.length - 1) {
        System.out.print(", ");
    }
}
System.out.println("]");

String[] strArray = new String[] {"John", "Mary", "Bob"};
System.out.print("[");
for (int i = 0; i < strArray.length; i++) {
    System.out.print(strArray[i]);
    if (i < strArray.length - 1) {
        System.out.print(", ");
    }
}
System.out.println("]");

In this approach, we use a loop to iterate through each element of the array and print it. We also add the necessary comma and space separators between elements to ensure the desired format is achieved.

While this method provides more control over the output format, it requires more manual coding compared to the Arrays.toString() method.

Method 3: Using StringBuilder

Another approach to print a Java array in a formatted way is by using a StringBuilder. By appending the array elements to the StringBuilder object and manipulating the separators, we can create the desired output format.

int[] intArray = new int[] {1, 2, 3, 4, 5};
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < intArray.length; i++) {
    sb.append(intArray[i]);
    if (i < intArray.length - 1) {
        sb.append(", ");
    }
}
sb.append("]");
System.out.println(sb.toString());

String[] strArray = new String[] {"John", "Mary", "Bob"};
sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < strArray.length; i++) {
    sb.append(strArray[i]);
    if (i < strArray.length - 1) {
        sb.append(", ");
    }
}
sb.append("]");
System.out.println(sb.toString());

In this method, we use a StringBuilder object to build the final string representation of the array. By appending the array elements and separators to the StringBuilder, we create the desired format. Finally, we convert the StringBuilder to a string using the toString() method.

This approach offers more flexibility than a simple loop, as we can easily customize the separators or add additional formatting if needed.

Conclusion

Printing a Java array in a formatted way can be achieved in several ways. The simplest approach is to use the Arrays.toString() method, which is specifically designed for this purpose. However, if you need more control over the output format, you can use a loop or a StringBuilder to manually manipulate the array elements and separators.

By employing these methods, you can easily print arrays of both primitive types and object references in a readable and organized format.