📜  Java中的 Arrays.deepToString() 示例

📅  最后修改于: 2022-05-13 01:54:59.031000             🧑  作者: Mango

Java中的 Arrays.deepToString() 示例

Java.util.Arrays.deepToString(Object[]) 方法是一个Java.util.Arrays 类方法。

返回指定数组的“深层内容”的字符串表示形式。如果数组包含其他数组作为元素,则字符串表示包含它们的内容等等。此方法旨在将多维数组转换为字符串。简单的 toString() 方法适用于简单数组,但不适用于多维数组。此方法旨在将多维数组转换为字符串。

句法:

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]]

打印多维数组

// A Java program to print 2D array using deepToString()
import java.util.Arrays;
  
public class GfG
{
    public static void main(String[] args)
    {
        // Create a 2D array
        int[][] mat = new int[2][2];
        mat[0][0] = 99;
        mat[0][1] = 151;
        mat[1][0] = 30;
        mat[1][1] = 5;
  
        // print 2D integer array using deepToString()
        System.out.println(Arrays.deepToString(mat));
    }
}

输出:

[[99, 151], [30, 5]]

toString() 与 deepToString()
toString() 适用于一维数组,但不适用于多维数组。

// Java program to demonstrate that toString works if we 
// want to print single dimensional array, but doesn't work
// for multidimensional array.
import java.util.Arrays;
public class Deeptostring
{
    public static void main(String[] args)
    {
        // Trying to print array of strings using toString
        String[] strs = new String[] {"practice.geeksforgeeks.org",
                                      "quiz.geeksforgeeks.org"
                                     };
        System.out.println(Arrays.toString(strs));
  
        // Trying to print multidimensional array using
        // toString
        int[][] mat = new int[2][2];
        mat[0][0] = 99;
        mat[0][1] = 151;
        mat[1][0] = 30;
        mat[1][1] = 50;
        System.out.println(Arrays.toString(mat));
    }
}

输出:

[practice.geeksforgeeks.org, quiz.geeksforgeeks.org]
[[I@15db9742, [I@6d06d69c]

注意:我们可以使用循环来使用 deepToString() 打印多维数组的内容。

deepToString() 适用于单维和多维,但不适用于单维基元数组

// Java program to demonstrate that deepToString(strs))
// works for single dimensional arrays also, but doesn't
// work single dimensional array of primitive types.
import java.util.Arrays;
public class Deeptostring
{
    public static void main(String[] args)
    {
        String[] strs = new String[] {"practice.geeksforgeeks.org",
                                      "quiz.geeksforgeeks.org"
                                     };
        System.out.println(Arrays.deepToString(strs));
          
        Integer []  arr1 = {10, 20, 30, 40};
        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));   */    
    }
}

输出:

[practice.geeksforgeeks.org, quiz.geeksforgeeks.org]
[10, 20, 30, 40]

参考:

https://docs.oracle.com/javase/7/docs/api/ Java Java.lang.Object[])