Print an array in Java

Write a program to print an single-dimensional and multi-dimensional arrays in java.

 

Printing 1D arrays –

1. Using For Loop or “Enhanced” For Loop
2. Using Arrays.toString() or Arrays.deepToString() method
3. Apache Commons Lang: ArrayUtils.toString() method
4. Java 8: Stream
5. Using Arrays.asList() or Guava’s Ints.asList()

 

1. Using For Loop or “Enhanced” For Loop

Naive approach will be to use a for loop as shown below:

Java

Download Run Code

In Java SE 5 and above, we can replace above for loop by enhanced for loop as shown below –

Download Run Code

 

2. Using Arrays.toString() or Arrays.deepToString() method

Another simple solution would be to just 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.

Java

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. Below 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 the a comma (and not followed by a space).
     
  3. Support for printing Multi-dimensional arrays is provided.
     

Java

Download

4. Java 8: Stream

In Java SE 8 and above, we can use streams to print an array. Below are the two terminal operations we can apply to a stream in order 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);
     

Java

Download Run Code

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

For array of non-primitive objects, we can use Arrays.asList() as shown below. Arrays.asList() returns fixed-size list backed by the specified array. This approach is not recommended as it includes creation of an List as 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

Printing 2D arrays –

1. For Loop / “Enhanced” For Loop
2. Using Arrays.toString() or Arrays.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 next 1D array and one to iterate through that array.

Java

Download Run Code

In Java SE 5 and above, we can replace above for loop by enhanced for loop as shown below –

Download Run Code

 

2. Using Arrays.toString() or Arrays.deepToString() method

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

Java

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.

Java

Download

Output:

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

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz