Open In App

Arrays.deepToString() in Java with Example

Last Updated : 25 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The Arrays.deepToString() method comes under the Arrays class in Java. It is used to return a string representation of the “deep contents” of a specified array. It handles multidimensional arrays by including the contents of nested arrays.

Example:

Below is a simple example that uses Arrays.deepToString() to convert a multidimensional array into a readable string format.

import java.util.Arrays;

public class Example {
    public static void main(String[] args) {
      
        int[][] arr = { {1, 2, 3}, 
                        {4, 5, 6}, 
                        {7, 8, 9} };
        
        // using Arrays.deepToString() to 
        // display the array content
        System.out.println("" + Arrays.deepToString(arr));
    }
}

Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Explanation: In this example, the Arrays.deepToString() method prints a 2D array, to get a readable format without manually iterating through the elements.

Syntax of Arrays.deepToString() method

public static String deepToString(Object[] arr)

Parameter: arr: An array whose string representation is needed. It can be a single-dimensional or multi-dimensional array.

Return Type:

  • This method returns string representation of array.
  • If the array contains other arrays, it prints the elements of those arrays as well.
  • If the array is null, the method returns the string "null".

Examples of Arrays.deepToString() in Java

In this example, we will use the Arrays.deepToString() method to print a two-dimensional array, which will display the nested elements correctly.

// Java program to print 2D array 
// using deepToString()
import java.util.Arrays;

public class GfG {
    public static void main(String[] args) {
        
        int[][] a = new int[2][2];
        a[0][0] = 11;
        a[0][1] = 22;
        a[1][0] = 33;
        a[1][1] = 44;

        // print 2D integer array 
        // using deepToString()
        System.out.println(Arrays.deepToString(a));
    }
}

Output
[[11, 22], [33, 44]]


Use deepToString() with Single and Multidimensional Arrays

Arrays.deepToString() method works for both single and multidimensional arrays, but does not support primitive type arrays such as int[].

// Java program to demonstrate deepToString() works for 
// single-dimensional arrays, but not for primitive arrays
import java.util.Arrays;

public class GFG {
    public static void main(String[] args) {
        
      String[] s = new String[] {"practice.geeksforgeeks.org",
                                      "www.geeksforgeeks.org"
                                     };
        System.out.println(Arrays.deepToString(s));
        
        Integer []  arr1 = {11, 22, 33, 44};
        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]
[11, 22, 33, 44]

toString() vs. deepToString()

  • toString() method doesn’t work well for multidimensional arrays.
  • deepToString() method prints multidimensional arrays properly.


Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg