sixtytrees sixtytrees - 4 months ago 7
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

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

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