2016-07-24 58 views
0

我想傳遞一個布爾值數組到一個方法。此代碼的工作:如何將新數組作爲參數傳遞給Java中的方法?

void checkResults(boolean[] isChecked){ 
    //Do something 
} 

    boolean[] isChecked= {true, true}; 
    checkResults(isChecked); //works 

但下面的所有努力都失敗了:

 checkResults(new {true, true}); //Compile time error 
    checkResults({true, true});  //Compile time error 
    checkResults(true, true);  //Compile time error (this one is obvious) 

有沒有一種方法來創建參數數組並傳遞給方法在一條線?

+2

你試過checkResults(new boolean [] {true,true})嗎? – VatsalSura

回答

3

您可以像這樣創建一個匿名數組並傳遞它。

checkResults(new boolean[]{true, true}); 
相關問題