Java – How to print an Array

In this article, we will show you a few ways to print a Java Array.

1. Arrays.toString

This is the simplest way to print an Array – Arrays.toString (since JDK 1.5)

PrintArray.java

package com.mkyong.utils.print;

import java.util.Arrays;

public class PrintArray {

    public static void main(String[] args) {

		// array
        String[] arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
        System.out.println(Arrays.toString(arrayStr));
        // Output : [Java, Node, Python, Ruby]

		int[] arrayInt = {1, 3, 5, 7, 9};
        System.out.println(Arrays.toString(arrayInt));
        // Output : [1, 3, 5, 7, 9]

        // 2d array, need Arrays.deepToString
        String[][] deepArrayStr = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}};
        System.out.println(Arrays.toString(deepArrayStr));
        // Output : [[Ljava.lang.String;@23fc625e, [Ljava.lang.String;@3f99bd52]

        System.out.println(Arrays.deepToString(deepArrayStr));
        // Output : [[mkyong1, mkyong2], [mkyong3, mkyong4]

        int[][] deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
        System.out.println(Arrays.toString(deepArrayInt));
        // Output : [[I@3a71f4dd, [I@7adf9f5f]

        System.out.println(Arrays.deepToString(deepArrayInt));
        // Output : [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

    }

}

Output

[Java, Node, Python, Ruby]
[1, 3, 5, 7, 9]
[[Ljava.lang.String;@23fc625e, [Ljava.lang.String;@3f99bd52]
[[mkyong1, mkyong2], [mkyong3, mkyong4]]
[[I@3a71f4dd, [I@7adf9f5f]
[[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

2. JDK 8 Stream

In JDK 8, we can convert it to Stream and print it.

PrintArray.java

package com.mkyong.utils.print;

import java.util.Arrays;

public class PrintArray {

    public static void main(String[] args) {

        // array
        String[] arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
        Arrays.stream(arrayStr).forEach(System.out::println);

        int[] arrayInt = {1, 3, 5, 7, 9};
        Arrays.stream(arrayInt).forEach(System.out::println);

        //2d array
        String[][] deepArrayStr = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}};
        Arrays.stream(deepArrayStr).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);

        int[][] deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
        Arrays.stream(deepArrayInt).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);

    }

}

Output

Java
Node
Python
Ruby
1
3
5
7
9
mkyong1
mkyong2
mkyong3
mkyong4
1
3
5
7
9
2
4
6
8
10

3. Json Anything

This is my favor method, use Jackson2 library to print anything in a JSON formatted string.

PrintUtils.java

package com.mkyong.utils.print;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PrintUtils {

    private static final ObjectMapper om = new ObjectMapper();

	public static void print(Object object) {
		String result;
		try {
			result = om.writerWithDefaultPrettyPrinter().writeValueAsString(object);
            System.out.println(result);
        } catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	}
	
}
PrintArray.java

package com.mkyong.utils.print;

public class PrintArray {

    public static void main(String[] args) {

		//array
        String[] arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
        PrintUtils.print(arrayStr);

        int[] arrayInt = {1, 3, 5, 7, 9};
        PrintUtils.print(arrayInt);

		//2d array
        String[][] deepArrayStr = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}};
        PrintUtils.print(deepArrayStr);

        int[][] deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
        PrintUtils.print(deepArrayInt);

    }

}

Output

[ "Java", "Node", "Python", "Ruby" ]
[ 1, 3, 5, 7, 9 ]
[ [ "mkyong1", "mkyong2" ], [ "mkyong3", "mkyong4" ] ]
[ [ 1, 3, 5, 7, 9 ], [ 2, 4, 6, 8, 10 ] ]

References

  1. Steam Javasoc
  2. Arrays JavaDoc
  3. Jackson 2 convert Java object to from JSON
  4. Jackson 2 official site

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Tokala Sai Tejaanurag Recent comment authors
newest oldest most voted
Tokala Sai Teja
Guest
Tokala Sai Teja

public static void printArray(Object[] R){
System.out.println(Arrays.deepToString(R).replaceAll(“],”, “],” + System.getProperty(“line.separator”)));
}

anurag
Guest
anurag

this article is very helpful for everyone who want to print an array.
thank you for this nice post