【问题标题】:How to pass a new array as arguments to a method in Java?如何将新数组作为参数传递给 Java 中的方法?
【发布时间】:2016-07-24 01:50:54
【问题描述】:

我正在尝试将一个布尔值数组传递给一个方法。此代码有效:

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)

有没有办法在参数中创建一个数组并在一行中传递给一个方法?

【问题讨论】:

  • 你试过checkResults(new boolean[]{true, true})吗?

标签: java arrays arguments


【解决方案1】:

你可以像这样创建一个匿名数组并传递它。

checkResults(new boolean[]{true, true});

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 2012-07-05
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多