How to print an array in Java in a readable format?
In Java, arrays don’t override toString()
, so printing an array directly results in output like className + '@' + hex hashCode
, as defined by Object.toString()
:
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // Prints something like '[I@3343c8b3'
But typically, we’d want output like [1, 2, 3, 4, 5]
. How to print an array in Java in a simple and readable way? Here are some example inputs and expected outputs:
// Array of primitives:
int[] intArray = {1, 2, 3, 4, 5};
// Expected output: [1, 2, 3, 4, 5]
// Array of object references:
String[] strArray = {"John", "Mary", "Bob"};
// Expected output: [John, Mary, Bob]
From my experience, the easiest way to print an array in Java in a readable format is by using Arrays.toString()
. It’s simple, effective, and works with both primitive arrays and object arrays.
Here’s an example:
import java.util.Arrays;
public class PrintArray {
public static void main(String[] args) {
int[] intArray = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(intArray)); // [1, 2, 3, 4, 5]
String[] strArray = {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(strArray)); // [John, Mary, Bob]
}
}
Pros:
- Works with primitive arrays and object arrays.
- It’s a simple one-liner.
- Produces readable output.
It’s a solid go-to for most cases when you need to print an array in Java in a quick and straightforward way.
Definitely agree with @yanisleidi-rodriguez Arrays.toString()
is great, but it won’t handle multi-dimensional arrays very well. You’ll see something like [I@hashcode
, which isn’t all that helpful.
If you’re working with multi-dimensional arrays, I’d recommend using Arrays.deepToString()
instead. Here’s how you can use it:
import java.util.Arrays;
public class PrintMultiArray {
public static void main(String[] args) {
int[][] multiArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(Arrays.deepToString(multiArray)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
}
}
Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Pros:
- Handles multi-dimensional arrays.
- Outputs in a clean, structured format.
So if you’re dealing with something more complex than a flat array, this will do the trick for how to print an array in Java."
You’re right, @ian-partridge But if you want even more flexibility, like when you need a custom format, such as {1, 2, 3, 4, 5}
instead of the default [1, 2, 3, 4, 5]
, Java Streams are a great option. You can use them to format the array exactly the way you need.
Check this out:
import java.util.Arrays;
import java.util.stream.Collectors;
public class PrintWithStreams {
public static void main(String[] args) {
int[] intArray = {1, 2, 3, 4, 5};
String formatted = Arrays.stream(intArray)
.mapToObj(String::valueOf)
.collect(Collectors.joining(", ", "{", "}"));
System.out.println(formatted); // {1, 2, 3, 4, 5}
}
}
Pros:
- Fully customizable format.
- Works great for logging or when you need outputs similar to JSON formatting.
It’s a super handy tool if you’re looking for more control over how to print an array in Java, especially for specific formatting or when you want a cleaner, more visually distinct output.