Java Arrays - deepToString() Method



Description

The Java Arrays deepToString(Object[]) 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 string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

Declaration

Following is the declaration for java.util.Arrays.deepToString() method

public static String deepToString(Object[] a)

Parameters

a − This is the array whose string representation to return

Return Value

This method returns a string representation of a.

Exception

  • NA

Example 1

The following example shows the usage of Java Arrays deepToString(Object[]) method. In this example, we've created one array of Strings and printed the string version of it using deepToString() method.

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
  public static void main(String[] args) {

      //initializing an string array
      String[] stringArr = { "tuts","point" };	  
      System.out.println("Deep String version: ");

      // printing value
      System.out.println(Arrays.deepToString(stringArr));
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Deep String version: 
[tuts, point]

Example 2

The following example shows the usage of Java Arrays deepToString(Object[]) method. In this example, we've created one array of Integers and printed the string version of it using deepToString() method.

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
  public static void main(String[] args) {

      //initializing an integer array
      Integer[] intArr = { 1, 2 };	  
      System.out.println("Deep String version: ");

      // printing value
      System.out.println(Arrays.deepToString(intArr));
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Deep String version: 
[1, 2]

Example 3

The following example shows the usage of Java Arrays deepToString(Object[]) method. In this example, we've created one array of Student objects and printed the string version of it using deepToString() method.

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
  public static void main(String[] args) {

      //initializing an Student array
      Student[] studentArr = { new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam") };
      System.out.println("Deep String version: ");

      // printing value
      System.out.println(Arrays.deepToString(studentArr));
   }
} 
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Deep String version: 
[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arrays.htm
Advertisements