[英]How to pass a new array as arguments to a method in Java?
I am trying to pass an array of boolean values to a method. 我正在尝试将布尔值数组传递给方法。 This code works:
此代码有效:
void checkResults(boolean[] isChecked){
//Do something
}
boolean[] isChecked= {true, true};
checkResults(isChecked); //works
But all attempts below failed: 但是以下所有尝试均失败:
checkResults(new {true, true}); //Compile time error
checkResults({true, true}); //Compile time error
checkResults(true, true); //Compile time error (this one is obvious)
Is there a way to create an array in arguments and pass to a method in one line? 有没有一种方法可以在参数中创建数组并在一行中传递给方法?
您可以像这样创建一个匿名数组并传递相同的内容。
checkResults(new boolean[]{true, true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.