Learn how to print array in Java using Arrays. Displaying array either int or string or any another type is straight forward by using java.util.Arrays
java utility class.
1. Using Arrays.toString and Arrays.deepToString
To print array in Java using Arrays i.e. java.util.Arrays
. You should choose utility class or library, if there is not compelling reason to chose your custom approach. You know performance of library or utility will be good comparatively your custom approach most of the time.
- Use java utility method
toString()
from utility classjava.util.Arrays
,if arrays is single dimensional and - Use java utility method deepToString() from utility class
java.util.Arrays
,if arrays is mulit-dimensional .
package com.mysoftkey.java.util; import java.util.Arrays; /** * This java class is used to display my custome int array or string array Using * Arrays.toString() and Arrays.deepToString utility methods. * * * @author Ranjeet Jha * */ public class PrintMyArrayExample { public static void main(String[] args) { int[] myIntAray = { 1, 8, 5, 2, 9 }; System.out.println("Using Arrays.toString() method : "); System.out.println(Arrays.toString(myIntAray)); /* * if use simple Arrays.toString() method on multi dimensional , * you get address of reference, not value, * you have to use Arrays.deepToString() method to print nested array. */ int[][] deepArrayInt = new int[][] { { 1, 3 }, { 29, 40, 878 } }; System.out.println(" \n Using Arrays.toString() method on multidimensional array: "); System.out.println(Arrays.toString(deepArrayInt)); System.out.println(" \n Using Arrays.deepToString() method on multi-dimensional array: "); System.out.println(Arrays.deepToString(deepArrayInt)); /* * now we have taken String[] array with single and multi-dimensional */ String[] myStrArray = { "Java", "Spring", "Hibernate" }; System.out.println("\n Using Arrays.toString() method: "); System.out.println(Arrays.toString(myStrArray)); // 2d or 2 dimensional array, using Arrays.deepToString String[][] deepArrayStr = new String[][] { { "java", "j2ee" }, { "spring", "hibernate" } }; System.out.println(" \n Using Arrays.toString() on multi-dimensional array, which print object reference: "); System.out.println(Arrays.toString(deepArrayStr)); System.out.println(" \n Using Arrays.deepToString() method on multi-dimensional array: "); System.out.println(Arrays.deepToString(deepArrayStr)); } }
2. Program Output
Using Arrays.toString() method : [1, 8, 5, 2, 9] Using Arrays.toString() method on multidimensional array: [[I@15db9742, [I@6d06d69c] Using Arrays.deepToString() method on multi-dimensional array: [[1, 3], [29, 40, 878]] Using Arrays.toString() method: [Java, Spring, Hibernate] Using Arrays.toString() on multi-dimensional array, which print object reference: [[Ljava.lang.String;@4e25154f, [Ljava.lang.String;@70dea4e] Using Arrays.deepToString() method on multi-dimensional array: [[java, j2ee], [spring, hibernate]]
3. Reference
I hope you enjoyed this post to print array in Java using Arrays utility class java.util.Arrays
, you can visit visit Core Java tutorial for more blog post.
Happy Learning! 🙂
Please write your suggestions to improve this post. Happy Learning! 🙂
Pingback: Michal
Pingback: sweety