Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to a Java method

I need to pass arguments to a java method func(String, String...). Problem is I have all the strings I need to pass as arguments, but they are in an ArrayList container.

For Example:

for(...some condition...)
{
    //the other method returns an ArrayList with different size in each iteration
    ArrayList <String> values = someOtherMethod(); 
    func(values) //values is an ArrayList<String>, containing all the arguments
}

I get an error saying that ArrayList<String> can't be converted to String. The size of values changes, because the someOtherMethod method returns a container with different size each iteration, so I can't pass values[0], values[1], etc as arguments.

I'm guessing the func wants an X amount of String variables, but I don't know how to convert a container to an X amount of variables when the size of the container isn't constant.

like image 352
George Georgiev Avatar asked May 26 '26 21:05

George Georgiev


1 Answers

The "..." operator accepts arrays, so you could just do the following:

ArrayList <String> values = someOtherMethod(); 
func(values.toArray(new String[values.size()]);
like image 116
Mureinik Avatar answered May 28 '26 10:05

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!