Print an array in Java
Write a program to print single-dimensional and multidimensional arrays in Java.
How to print 1D Arrays?
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } } |
We can replace the above for-loop by enhanced for-loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 |
class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; for (int value : arr) { System.out.println(value); } } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; class Main { public static void main(String[] args) { Integer[] arr = { 1, 2, 3, 4, 5 }; System.out.println(Arrays.toString(arr)); System.out.println(Arrays.deepToString(arr)); } } |
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():
- The string representation of the array’s elements is enclosed in curly braces
{}as opposed to square brackets[]. - The adjacent elements are separated only by a comma (and not, followed by a space).
- Support for printing Multi-dimensional arrays is provided.
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.apache.commons.lang3.ArrayUtils; class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(ArrayUtils.toString(arr)); } } |
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.
- Get iterator to the stream
Iterator itr = Arrays.stream(arr).iterator();. - Using Stream.forEach
Arrays.stream(arr).forEach(System.out::println);.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; Arrays.stream(arr).forEach(System.out::println); } } |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.Iterator; class Main { public static void main(String[] args) { Integer[] arr = { 1, 2, 3, 4, 5 }; Iterator<Integer> itr = Arrays.asList(arr).iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } } |
Guava
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.primitives.Ints; import java.util.List; class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; List<Integer> itr = Ints.asList(arr); itr.forEach(System.out::println); } } |
How to print 2D Arrays?
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {1, 2, 3, 4, 5}, {1}, {1, 2} }; for (int i = 0; i < arr.length; i++) { for (int j = 0; arr[i] != null && j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } } |
We can also replace the above for-loop by enhanced for-loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {1, 2, 3, 4, 5}, {1}, {1, 2} }; for (int[] row: arr) { if (row != null) // important check for (int val: row) { System.out.print(val + " "); } System.out.println(); } } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {1, 2, 3, 4, 5}, {1}, {1, 2} }; for (int[] ints : arr) { System.out.println(Arrays.toString(ints)); } } } |
Output:
[1, 2, 3]
[1, 2, 3, 4, 5]
[1]
[1, 2]
Here’s an equivalent version using the Stream API.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {1, 2, 3, 4, 5}, {1}, {1, 2} }; Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.apache.commons.lang3.ArrayUtils; class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {1, 2, 3, 4, 5}, {1}, {1, 2} }; System.out.println(ArrayUtils.toString(arr)); } } |
Output:
{{1,2,3},{1,2,3,4,5},{1},{1,2}}
That’s all about printing an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)