Passing a List in as varargs

xbakesx picture xbakesx · Aug 31, 2012 · Viewed 53.8k times · Source

I have a List<Thing> and I would like to pass it to a method declared doIt(final Thing... things). Is there a way to do that?

The code looks something like this:

public doIt(final Thing... things)
{
    // things get done here
}

List<Thing> things = /* initialized with all my things */;

doIt(things);

That code obviously doesn't work because doIt() takes Thing not List<Thing>.

Is there a way to pass in a List as the varargs?

This is in an Android App, but I don't see why the solution will not apply to anything Java

Answer

SLaks picture SLaks · Aug 31, 2012

Just pass things.toArray(new Thing[things.size()]).