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

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.