Explain Codes LogoExplain Codes Logo

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

java
array-printing
debugging
java-8
Nikita BarsukovbyNikita Barsukov·Mar 12, 2025
TLDR

Quickly visualize a Java array using Arrays.toString() for single-dimensional arrays:

// Say 'Hello, World!' to our adorable little array here System.out.println(Arrays.toString(new int[] {1, 2, 3}));

If dealing with Inception-like nested arrays, they are best handled by Arrays.deepToString():

// Go deeper... use deepToString() a.k.a the 'Inception' method! System.out.println(Arrays.deepToString(new int[][] {{1, 2}, {3, 4}}));

These methods print array elements into a formatted string that's ready for your disposal.

Quick guide to effective array printing

Understanding the pros and cons of different methods to print arrays can step up your debugging game. Let's get started!

Leave Object.toString() for the cryptologists

Using Object.toString() on arrays will land you into cryptic territory. Unless you're a fan of deciphering codes like [I@3343c8b3, avoid it!

Arrays.toString() - The knight in shining armor for primitive arrays

When dealing with primitive arrays such as int[] or double[], let Arrays.toString() come to your rescue by printing them as beautifully formatted strings.

Custom formatting with Stream API

Craving something more customizable? The Stream API in Java 8+ has got you covered!

IntStream.of(new int[] {1, 2, 3}) .mapToObj(Integer::toString) .collect(Collectors.joining(", "));

This beauty converts your primitive array to a string and separates the elements with a comma and space, all in one go. Talk about being snazzy!

Embrace the power of lambdas and method references

Step into the world of lambdas and method references to add a sprinkle of elegance to your array printing. Thanks to .boxed() and Stream.of(), you can magic any array type into a stream and manipulate it with the functional API.

//Voila! Every element gets its own line. No pushing, folks! Stream.of(array).forEach(System.out::println);

Journey into the deep with deepToString

Is your array having an Inception moment with layers within layers? That's when Arrays.deepToString() becomes your best friend, providing a clear visualization of nested structures.

// Unleash the 'Inception' method! System.out.println(Arrays.deepToString(new String[][]{{"John", "Mary"}, {"Alice", "Bob"}}));

The output is now a matrix-like representation [[John, Mary], [Alice, Bob]]

Array enlightenment - real-world use cases

Reality check - how do these methods fare in real-world scenarios?

Array Conversion

Got a need to convert an array to a List? Java's got a shorthand for this:

// Because sometimes, arrays need a little List therapy Arrays.asList(array).forEach(System.out::println);

Sublime Lambda Custom Formatting

Want to achieve a specific format while printing your arrays? Welcome to the lambda club:

// Arrays have joined String. It was super effective! String.join(", ", Stream.of(array).map(Object::toString).toArray(String[]::new));

Note: Arrays used lambda!

Keeping it clean

Lastly, don't forget to import java.util.Arrays to use Arrays. Keep your imports tidy to avoid a mess later on.