2013-08-05 45 views
2

這是Android Developer's Website這裏正在做什麼樣的數組初始化(Basic Java)?

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
try { 
    ndef.addDataType("*/*"); /* Handles all MIME based dispatches. 
            You should specify only the ones that you need. */ 
} 
catch (MalformedMimeTypeException e) { 
    throw new RuntimeException("fail", e); 
} 
intentFiltersArray = new IntentFilter[] {ndef, }; 

所以在這裏,intentFiltersArray[0] = ndef。那麼intentFiltersArray[1]及更高版本呢?在上面的代碼中ndef的含義是什麼,

再次,它有怎樣techListsArray[][]這裏初始化另一個代碼示例作爲

techListsArray = new String[][] { new String[] { NfcF.class.getName() } }; 

?我猜techListsArray[0][0]=NfcF.class.getName()(應該是NfcF不應該嗎?)但其他元素呢?還是它只有一個單一的元素?

回答

5

所以在這裏,intentFiltersArray[0] = ndef。那麼intentFiltersArray[1]及更高版本呢?

[1]或「超越」沒有元素。

您正在創建一個元素的數組。如果您嘗試訪問intentFiltersArray[1],您將得到一個未經檢查的例外:ArrayIndexOutOfBoundsException

在上面的代碼中,ndef代表什麼意思?

這意味着什麼。 Java語言語法允許在數組初始值設定項列表末尾添加一個冗餘逗號。 (從表面上看,這是使源代碼生成更容易...)


現在你的第二個例子:techListsArray如何初始化這裏

[] []?

它被初始化爲字符串的陣列的一個1x1數組:

  • techListsArray[0]String[]與一種元素。
  • techListsArray[0][0]是一個字符串... 完全合格的NfcF類的名稱;例如"some.pkg.NfcF"
1
intentFiltersArray = new IntentFilter[] {ndef, }; 

是相同

intentFiltersArray = new IntentFilter[] {ndef}; 

,現在你的問題是如何初始化

IntentFilter[] intentFiltersArray = new IntentFilter[] {ndef }; 

這將創建的IntentFilter的ONW走的數組。它將定義並聲明數組爲單行,數組大小爲1,因爲只有1個元素ndef