当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Arrays.deepToString()用法及代码示例


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

返回指定数组的“deep contents”的字符串表示形式。如果数组包含其他数组作为元素,则字符串表示形式包含其内容,依此类推。此方法旨在将多维数组转换为字符串。简单的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/util/Arrays.html#deepToString(java.lang.Object[])



相关用法


注:本文由纯净天空筛选整理自 Arrays.deepToString() in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。