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.
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.
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
[[99, 151], [30, 5]]
toString() vs deepToString()
toString() works well for single dimensional arrays, but doesn’t work for multidimensional arrays.
[practice.geeksforgeeks.org, quiz.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
[practice.geeksforgeeks.org, quiz.geeksforgeeks.org] [10, 20, 30, 40]
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java 8 | DoubleToIntFunction Interface in Java with Example
- NavigableMap Interface in Java with Example
- Deque interface in Java with Example
- Swapping items of a list in Java : Collections.swap() with Example
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Collections.singleton() method in Java with example
- java.util.Currency methods with example
- Beginning Java programming with Hello World Example
- Serialization and Deserialization in Java with Example
- Java Math asin() method with Example
- Map.Entry interface in Java with example
- Arraylist lastIndexOf() in Java with example
- ArrayList trimToSize() in Java with example
- ArrayList isEmpty() in Java with example
- Java String isEmpty() method with example
- Java String contains() method with example
- Java Math log() method with example
- Java String charAt() method with example
- Java String toCharArray() with example
- Java log10() with example