Write a program to print single-dimensional and multidimensional arrays in Java.

1. For-loop or Enhanced for-loop
2. Arrays class – toString() or deepToString() method
3. Apache Commons Lang – ArrayUtils.toString() method
4. Java 8 Stream
5. Arrays.asList() or Guava’s Ints.asList()

1. For-loop or Enhanced for-loop

A naive approach will be to use a for-loop, as shown below:

Download  Run Code

 
We can replace the above for-loop by enhanced for-loop, as shown below:

Download  Run Code

2. Arrays class – toString() or deepToString() method

Another simple solution would be just to use Arrays.toString(Object[] a) method that will return a string representation of the contents of the specified array. We can also use Arrays.deepToString(Object[] a) that returns a string representation of the “deep contents” of the specified array, i.e., if the array contains other arrays as elements, the string representation includes their contents and so on.

Download  Run Code

Output:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

3. Apache Commons Lang – ArrayUtils.toString() method

Arrays.toString() and Arrays.deepToString() will work only on Java SE 5 and above. Apache Commons provides ArrayUtils.toString() method, which is similar to Arrays.deepToString() and outputs an array as a String. Following are its differences with Arrays.deepToString():

  1. The string representation of the array’s elements is enclosed in curly braces {} as opposed to square brackets [].
  2. The adjacent elements are separated only by a comma (and not, followed by a space).
  3. Support for printing Multi-dimensional arrays is provided.

Download Code

4. Java 8 Stream

In Java SE 8 and above, we can use streams to print an array. Following are the two terminal operations we can apply to a stream to print an array.

  1. Get iterator to the stream Iterator itr = Arrays.stream(arr).iterator();.
  2. Using Stream.forEach
    Arrays.stream(arr).forEach(System.out::println);.

The following program demonstrates it:

Download  Run Code

5. Arrays.asList() or Guava’s Ints.asList()

For an array of non-primitive objects, we can use Arrays.asList() as shown below: Arrays.asList() returns a fixed-size list backed by the specified array. This approach is not recommended as it includes the creation of a list as an intermediate step.

Similar to Arrays.asList() method in JDK, Google’s Guava library has Ints.asList() that also returns a fixed-size list.

JDK


Download  Run Code

Guava


Download Code

1. For-loop / Enhanced for-loop
2. Arrays class – toString() or deepToString() method
3. Apache Commons Lang – ArrayUtils.toString() method

1. For-loop / Enhanced for-loop

The idea is to use two for-loops – one to get the next 1D array and one to iterate through that array.

Download  Run Code

 
We can also replace the above for-loop by enhanced for-loop, as shown below:

Download  Run Code

2. Arrays class – toString() or deepToString() method

We can use Arrays.toString(Object[] a) method to print string representation of a single-dimensional array only. For multidimensional arrays, it won’t work. We know that a multidimensional array is nothing but collections of one-dimensional arrays. So, we can make Arrays.toString() work by passing each one-dimensional array to it.

Download  Run Code

Output:

[1, 2, 3]
[1, 2, 3, 4, 5]
[1]
[1, 2]

 
Here’s an equivalent version using the Stream API.

Download  Run Code

Output:

[1, 2, 3]
[1, 2, 3, 4, 5]
[1]
[1, 2]

3. Apache Commons Lang – ArrayUtils.toString() method

As mentioned earlier, ArrayUtils.toString() provides support for Multi-dimensional primitive and non-primitive arrays.

Download Code

Output:

{{1,2,3},{1,2,3,4,5},{1},{1,2}}

That’s all about printing an array in Java.