當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。