📜  JavaArrays.toString() 和 Arrays.deepToString() 的区别

📅  最后修改于: 2021-09-10 02:56:56             🧑  作者: Mango

Arrays 类的deepToString()方法返回指定 Object 数组的深层内容的字符串表示形式。不像数组toString() ,如果数组包含其他数组作为元素,则字符串表示包括它们的内容,依此类推。

Arrays.toString() 返回指定数组内容的字符串表示形式。字符串表示由数组元素的列表组成,用方括号(“[]”)括起来。相邻元素由字符“, ”(逗号后跟空格)分隔。如果 a 为空,则返回“空”。

在对象数组的情况下,如果数组包含其他数组作为元素,它们会通过继承自 Object 的 Object.toString() 方法转换为字符串,该方法描述的是它们的身份而不是它们的内容。

  • 参数:要返回其字符串表示形式的数组。
  • 返回:数组的字符串表示形式。

Arrays.deepToString() : Java.util.Arrays.deepToString(Object[]) 方法是一个Java.util.Arrays 类方法。返回指定数组的“深层内容”的字符串表示形式。如果数组包含其他数组作为元素,则字符串表示包含它们的内容,依此类推。此方法旨在将多维数组转换为字符串。简单的 toString() 方法适用于简单数组,但不适用于多维数组。此方法旨在将多维数组转换为字符串。

句法:

JavaArrays.toString() 和 Arrays.toDeepString() 的区别

Arrays.toString()

Arrays.deepToString()

We use Arrays.toString() in java to get the String representation of Object We use Arrays.deepToString() method to get String Representation of Deep Content of the Specific array.
We widely use Arrays.toString() to return String() representation of single dimension Array. We can use Arrays.deepToString() method to return String representation of both single dimension and multi-dimension Arrays.
If the array contains other arrays as elements, the string representation does not contain their contents, and so on. If the array contains other arrays as elements, the string representation contains their contents, and so on.
We do not use Arrays.toString() method to return String representation of multi-Dimension Array. We can use Arrays.deepToString() method to return String representation of  multi-dimension Arrays.
Arrays.toString() work on arrays of primitives. Arrays.deepToString() does not work on Primitives.

下面是Arrays.toString() 和 Arrays.deepToString() 的图示

1. Integer 的一维数组。

Java
// Java program to show the usage of
// Arrays.toString() and Arrays.deepToString()
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        Integer[] a = { 1, 2, 3, 4, 5 };
        System.out.println("Using Arrays.toString(): "
                           + Arrays.toString(a));
  
        System.out.println("Using Arrays.deepToString(): "
                           + Arrays.deepToString(a));
    }
}


Java
// Java program to show the usage of
// Arrays.toString() and Arrays.deepToString()
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        int[] a = { 1, 2, 3, 4, 5 };
        System.out.println("Using Arrays.toString(): "
                           + Arrays.toString(a));
    }
}


Java
// Java program to show the usage of 
// Arrays.toString() and Arrays.deepToString()
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        Integer[] a1 = { 1, 2, 3, 4, 5 };
        Integer[] a2 = { 6, 7, 8, 9, 10 };
        Integer[][] multi = { a1, a2 };
        
        System.out.println("Using Arrays.toString(): "
                           + Arrays.toString(multi));
        
        System.out.println("Using Arrays.deepToString(): "
                           + Arrays.deepToString(multi));
    }
}


输出
Using Arrays.toString(): [1, 2, 3, 4, 5]
Using Arrays.deepToString(): [1, 2, 3, 4, 5]

2. Primitive(int) 的一维数组,其中只有 Arrays.toString() 起作用。

Java

// Java program to show the usage of
// Arrays.toString() and Arrays.deepToString()
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        int[] a = { 1, 2, 3, 4, 5 };
        System.out.println("Using Arrays.toString(): "
                           + Arrays.toString(a));
    }
}
输出
Using Arrays.toString(): [1, 2, 3, 4, 5]

3.多维数组:

Java

// Java program to show the usage of 
// Arrays.toString() and Arrays.deepToString()
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        Integer[] a1 = { 1, 2, 3, 4, 5 };
        Integer[] a2 = { 6, 7, 8, 9, 10 };
        Integer[][] multi = { a1, a2 };
        
        System.out.println("Using Arrays.toString(): "
                           + Arrays.toString(multi));
        
        System.out.println("Using Arrays.deepToString(): "
                           + Arrays.deepToString(multi));
    }
}
输出
Using Arrays.toString(): [[Ljava.lang.Integer;@3d075dc0, [Ljava.lang.Integer;@214c265e]
Using Arrays.deepToString(): [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]