In Java, Arrays.deepToString(a) returns a String for an array that may contain other arrays. Every inner array is printed with its own brackets, so a 2D array looks like [[1, 2], [3, 4]].
A 2D array in Java is really an array of row arrays. Arrays.toString stops at the first level and prints each row as [I@...; deepToString keeps going into each row, at any depth.
A static method of java.util.Arrays (Java 5+): call it on the class, as in Arrays.deepToString(...); there is no Arrays object to create.
Syntax
Pass any object array; nested arrays inside it are expanded.
Text for a 2D primitive array (it is an Object[] of rows).
Arrays.deepToString(String[][][] cube)
Works for any number of dimensions.
Arrays.deepToString(objects)
Mixed contents: plain values, arrays, and null.
Arrays.deepToString(null)
Returns "null".
There is only one overload, which takes Object[]. A one-dimensional primitive array such as int[] is not an Object[], so deepToString(new int[]{1}) does not compile; use toString for that.
Library declarations
Java
// java.util.Arrays (since Java 5)
public static String deepToString(Object[] a)
Parameters
Parameter
Description
a (Object[])
The array to describe. Its elements may themselves be arrays of any type, or null.
Return Value
Return Value
Description
String
The nested text, "[]" for an empty array, or "null" when a is null.
Different row lengths. Row A has three seats and row B has one; each row is printed with exactly the elements it has.
Empty versus missing. The third row is an empty array and prints as []; the fourth row is null and prints as the word null.
Three levels deep. For cube, the method goes from the outer array into each 2D block and then into each innermost int[].
Bracket depth shows dimensions. Three opening brackets at the start of the second line mean three levels of nesting.
Example 3 — Mixed Contents and an Array That Contains Itself
An Object[] can hold numbers, characters, arrays, null, and even itself.
Java
import java.util.Arrays;
public class ArraysDeepToStringSelf {
public static void main(String[] args) {
Object[] box = new Object[2];
box[0] = "me";
box[1] = box;
System.out.println(Arrays.deepToString(box));
Object[] mixed = {1, 'c', 2.5, new boolean[] {true}, null};
System.out.println(Arrays.deepToString(mixed));
System.out.println(Arrays.deepToString(null));
}
}
Output
[me, [...]]
[1, c, 2.5, [true], null]
null
How It Works
A loop in the data.box[1] = box makes the array contain itself, which would recurse forever if printed naively.
Cycle marker.deepToString remembers which arrays it is already printing and writes [...] for the repeat.
Boxed values and a primitive array. In mixed, 1, 'c', and 2.5 are autoboxed objects printed with String.valueOf, while the boolean[] is expanded to [true].
Only one overload.deepToString(null) compiles without a cast because there is a single Object[] version, and it returns null as text.
Frequently Asked Questions
toString prints one level, so the rows of a 2D array show up as [I@... text. deepToString goes into every nested array and prints the values.
Use Arrays.deepToString(matrix) for one line, or loop over the rows and print Arrays.toString(row) for one line per row.
No. An int[] is not an Object[], so the call does not compile. Use Arrays.toString for one-dimensional primitive arrays.
A null row is printed as the word null, while an empty row is printed as [].
It marks an array that contains itself. deepToString prints [...] instead of recursing forever.