简体   繁体   English

如何将新数组作为参数传递给Java中的方法?

[英]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.

相关问题 "我可以将数组作为参数传递给 Java 中具有可变参数的方法吗?" - Can I pass an array as arguments to a method with variable arguments in Java? Java:如何将动态数量的参数传递给方法 - Java: How to pass a dynamic amount of arguments to a method Java传递参数数组 - Java Pass Array of Arguments 如何在Java中传递数组而不是可变参数? - How to pass an array instead of variable arguments in java? 通过方法传递数组(java命令行参数) - pass array through method (java command line arguments) 如何将矩阵数组传递给Java方法参数列表整数变量error:error:没有为类找到具有匹配签名的方法“ list_method”? - How to Pass Matrix Array to Java Method arguments List Integer variable error:error:No method 'list_method' with matching signature found for class? 如何有条件地将Java中的参数传递给一个采用可变数量参数的方法? - How to conditionally pass parameters in Java to a method that takes a variable number of arguments? Java:如何将参数传递给构造函数? 未定义的方法错误? - Java: how to pass arguments into constructor? Undefined method error? 将数组传递给方法 Java - pass array to method Java 如何将数组和当前索引传递给Java中的方法 - How to pass an Array and the current index to a method in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM