Java How to print an Array
By:Roy.LiuLast updated:2019-08-17
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 mkyong1 mkyong2 mkyong3 mkyong4 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
From:一号门
COMMENTS