Java Arrays.deepToString(Object[] a) method example

Java Arrays.deepToString(Object[] a) method example

By technicalkeeda, Views 1115, Date 20-Feb-2018

In this tutorial, You will learn how to get a string representation the array. Java Arrays.deepToString(Object[] a) method returns a string representation.

tags Java

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). Elements are converted to strings as by String.valueOf(Object), unless they are themselves arrays.

If an element e is an array of a primitive type, it is converted to a string as by invoking the appropriate overloading of Arrays.toString(e). If an element e is an array of a reference type, it is converted to a string as by invoking this method recursively.

To avoid infinite recursion, if the specified array contains itself as an element, or contains an indirect reference to itself through one or more levels of arrays, the self-reference is converted to the string "[...]". For example, an array containing only a reference to itself would be rendered as "[[...]]".

This method returns "null" if the specified array is null.

syntax

public static String deepToString(Object[] a)

Parameters

a - the array whose string representation to return.

Returns

A string representation.

Java Arrays deepToString(Object[] a) example

  • Initialize the Object[][] array Object[][] array = { { 1, 2, 3, }, { 4, 5, 6 }, { 7, 8, 9 }, };
  • Arrays.deepToString(array); It returns a string representation of the "deep contents" of the specified array.
package com.technicalkeeda;

import java.util.Arrays;

public class App {

    public static void main(String[] args) {
        Object[][] array = {
            {
                1,
                2,
                3,
            },
            {
                4,
                5,
                6
            },
            {
                7,
                8,
                9
            },
        };

        System.out.println("The string representation of 2D array is :- ");

        System.out.println(Arrays.deepToString(array));
    }
}

output

The string representation of 2D array is :- 
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Suggested Posts

Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]