sixtytrees
sixtytrees

Reputation: 1233

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?

Upvotes: 0

Views: 56

Answers (1)

Ramachandran.A.G
Ramachandran.A.G

Reputation: 4948

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

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

Upvotes: 3

Related Questions