2017-09-26 98 views
0

今天我有2個疑問。 1)我試圖打印兩維陣列(矩陣NX),我使用這種方法:關於AtomicInteger和打印二維數組的疑問

System.out.println(Arrays.toString(Matr)); 

矩陣只有詮釋變量。

這是輸出,爲什麼?

[[[email protected], [[email protected], [[email protected], [[email protected], [[email protected], [[email protected], ........etc 

2)使用的AtomicIntegers我必須設置爲0所有矩陣我用這個代碼:

AtomicInteger[][]Matr=new AtomicInteger[n][m]; 

    for(int i=0; i<n; i++) { 
     for(int j=0; j<m; j++) { 
      Matr[i][j].set(0); 
     } 
    } 

但老師的解決方案是:

AtomicInteger[][] A = new AtomicInteger[n][m]; 
    for (int i = 0; i < A.length; i++) 
     for (int j = 0; j < A[i].length; j++) 
      A[i][j] = new AtomicInteger(0); 

是否有區別?我的代碼錯了嗎?

+0

你嘗試運行的代碼?你會看到不同之處。 – Eran

+0

好吧,看起來你有一個二維數組,這意味着兩個字符串將只將第一個維度轉換爲一個字符串,因爲它仍然包含數組,這不會產生太大的意義。您必須將第一維內的所有元素轉換爲字符串。另外,不要使用大寫變量名稱。 – ScriptKiddy

+0

太寬了。你已經問了兩個不相關的問題作爲一個問題。 –

回答

0

你的代碼會拋出一個空指針異常,因爲它試圖將值設置爲空對象。您必須首先初始化變量,然後設置值。

0

關於你的第一個問題使用