java.util.Arrays.deepToString(Object[]) method is a java.util.Arrays class method.
Returns a string representation of the “deep contents” of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings. The simple toString() method works well for simple arrays, but doesn’t work for multidimensional arrays. This method is designed for converting multi-dimensional arrays to strings.
Syntax:
public static String deepToString(Object[] arr) arr - An array whose string representation is needed This function returns string representation of arr[]. It returns "null" if the specified array is null.
Example:
Let us suppose that we are making a 2-D array of 3 rows and 3 column. 2 3 4 5 6 7 2 4 9 If use deepToString() method to print the 2-D array, we will get string representation as :- [[2,3,4], [5,6,7], [2,4,9]]
Printing multidimensional Array
// A Java program to print 2D array using deepToString() import java.util.Arrays; public class GfG { public static void main(String[] args) { // Create a 2D array int [][] mat = new int [ 2 ][ 2 ]; mat[ 0 ][ 0 ] = 99 ; mat[ 0 ][ 1 ] = 151 ; mat[ 1 ][ 0 ] = 30 ; mat[ 1 ][ 1 ] = 5 ; // print 2D integer array using deepToString() System.out.println(Arrays.deepToString(mat)); } } |
Output:
[[99, 151], [30, 5]]
toString() vs deepToString()
toString() works well for single dimensional arrays, but doesn’t work for multidimensional arrays.
// Java program to demonstrate that toString works if we // want to print single dimensional array, but doesn't work // for multidimensional array. import java.util.Arrays; public class Deeptostring { public static void main(String[] args) { // Trying to print array of strings using toString String[] strs = new String[] { "practice.geeksforgeeks.org" , "www.geeksforgeeks.org" }; System.out.println(Arrays.toString(strs)); // Trying to print multidimensional array using // toString int [][] mat = new int [ 2 ][ 2 ]; mat[ 0 ][ 0 ] = 99 ; mat[ 0 ][ 1 ] = 151 ; mat[ 1 ][ 0 ] = 30 ; mat[ 1 ][ 1 ] = 50 ; System.out.println(Arrays.toString(mat)); } } |
Output:
[practice.geeksforgeeks.org, www.geeksforgeeks.org] [[I@15db9742, [I@6d06d69c]
Note : We can use a loop to print contents of a multidimensional array using deepToString().
deepToString() works for both single and multidimensional, but doesn’t work single dimensional array of primitives
// Java program to demonstrate that deepToString(strs)) // works for single dimensional arrays also, but doesn't // work single dimensional array of primitive types. import java.util.Arrays; public class Deeptostring { public static void main(String[] args) { String[] strs = new String[] { "practice.geeksforgeeks.org" , "www.geeksforgeeks.org" }; System.out.println(Arrays.deepToString(strs)); Integer [] arr1 = { 10 , 20 , 30 , 40 }; System.out.println(Arrays.deepToString(arr1)); /* Uncommenting below code would cause error as deepToString() doesn't work for primitive types int [] arr2 = {10, 20, 30, 40}; System.out.println(Arrays.deepToString(arr2)); */ } } |
Output:
[practice.geeksforgeeks.org, www.geeksforgeeks.org] [10, 20, 30, 40]
Reference:
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepToString(java.lang.Object[])
This article is contributed by Mohit Gupta. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
.