In this article we will see how to pass an arraylist to a method expecting vararg as an method argument.

Suppose we have a method that accepts varargs of type Integer and calculates the sum of them i.e.

Now we can call this function by passing arguments separately i.e.

or by passing an array i.e.

Now suppose we want to pass an ArrayList to this method’s vararg parameter i.e.

It will give compile error because we can not directly pass ArrayList to vararg in method parameter. So, let’s see how to do this i.e.

Passing an ArrayList to method expecting vararg as parameter

To do this we need to convert our ArrayList to an Array and then pass it to method expecting vararg. We can do this in single line i.e.

Let’s see complete example as follows,


Other vararg related articles,

Java vararg Tutorial & Example

Click Here to Subscribe for more Articles / Tutorials like this.