sixtytrees sixtytrees - 1 year ago 57
Java Question

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?

Answer Source

You can create an anonymous array like this and pass the same.

checkResults(new boolean[]{true, true});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download