簡體   English   中英

使用Arrays.deepToString()從多維數組中的特定索引打印

[英]Printing from a specific index in a multidimensional array using Arrays.deepToString()

有沒有辦法使用Arrays.deepToString()從多維數組中打印特定值?

例如,我想在多維數組中的索引[1,1]處打印值。

我希望這是有道理的。

public class App {

    public static void main(String[] args) {

        int[][] a = {
                {10,20,30,40,50},{10,20,30,40,50}
        };

        System.out.println(Arrays.deepToString(a));
        System.out.println(Arrays.deepToString(a[1][1]));
    }    
}
System.out.println(a[1][1]);

打印:

20

數組中的deepToString(Object [])返回對象數組的字符串表示形式。 像:-

 int[][] a = {{10,20,30,40,50},{10,20,30,40,50}};

 System.out.println(Arrays.deepToString(a));

 Output:- [[10, 20, 30, 40, 50], [10, 20, 30, 40, 50]] // String representation of 'a'

deepToString()就是這個。

如果要打印特定元素,則使用數組的坐標,例如

System.out.println(a[1][2]);  //Output:-  30

簡短答案

是的,它有效。 您在對帖子的評論中注意到了這一點。

為什么有效

Arrays.deepToString似乎以遞歸方式擴展了它傳遞的任何數組(並在每個元素上調用toString方法)。 您的情況是“不是數組,只打印參數”的特殊情況。 在您的示例中,對deepToString的第二次調用是不必要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM