Java教程

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

本文主要是介绍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[])

这篇关于Java Arrays.deepToString()用法及代码示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!