ArrayList
Varargs Method
Java Programming
Coding Tips
Method Parameters

How to pass an ArrayList to a varargs method parameter?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Java, a varargs parameter is really just an array behind the scenes. An ArrayList is not an array, so you cannot pass it directly to a method that expects T... and expect Java to expand it automatically.

The fix is simple: convert the list to an array first. The more important point is understanding when varargs is the right API and when accepting a Collection would be cleaner.

Why an ArrayList Does Not Match Varargs

Suppose you have this method:

java
1public static void printItems(String... items) {
2    for (String item : items) {
3        System.out.println(item);
4    }
5}

This works:

java
printItems("Apple", "Banana", "Cherry");

But this does not:

java
1ArrayList<String> list = new ArrayList<>();
2list.add("Apple");
3list.add("Banana");
4
5printItems(list); // does not compile

That fails because Java sees an ArrayList<String>, not a String[].

Convert the List to an Array

The standard solution is toArray:

java
1import java.util.ArrayList;
2
3public class Main {
4    public static void printItems(String... items) {
5        for (String item : items) {
6            System.out.println(item);
7        }
8    }
9
10    public static void main(String[] args) {
11        ArrayList<String> list = new ArrayList<>();
12        list.add("Apple");
13        list.add("Banana");
14        list.add("Cherry");
15
16        printItems(list.toArray(new String[0]));
17    }
18}

This is the normal bridge from a list to a varargs method.

What Varargs Really Means

Varargs is just array syntax with a nicer calling form. The compiler turns:

java
printItems("Apple", "Banana");

into something conceptually similar to:

java
printItems(new String[] {"Apple", "Banana"});

Once you remember that, the toArray step becomes obvious. You are not converting to "varargs." You are converting to the array type that the varargs parameter actually uses.

Prefer a Collection Parameter When the Caller Already Has a List

Sometimes the best answer is to change the method signature instead of converting the ArrayList:

java
1import java.util.List;
2
3public static void printItems(List<String> items) {
4    for (String item : items) {
5        System.out.println(item);
6    }
7}

If most callers already have a List, this is often clearer and avoids unnecessary array creation. Varargs is best when callers naturally supply a small number of inline values.

That design choice matters for API readability too. A method that conceptually processes a collection usually reads better when the parameter type says so directly.

Generic Varargs Need Extra Care

If the method is generic, varargs can trigger warnings because arrays and generics interact awkwardly:

java
1public static <T> void printAll(T... items) {
2    for (T item : items) {
3        System.out.println(item);
4    }
5}

That does not change the ArrayList story, but it is one reason APIs that naturally consume collections often prefer Collection<T> parameters instead of generic varargs.

Common Pitfalls

The biggest mistake is expecting Java to unpack an ArrayList automatically into varargs. It will not. A list and an array are different types.

Another common issue is manually allocating an array of the wrong type or size. list.toArray(new String[0]) is the idiomatic and safe approach for this case.

Developers also sometimes keep a varargs API when every real caller already has a list. In that situation, a collection parameter is usually the cleaner design.

Finally, do not forget that toArray() without a typed array returns Object[], which is not the same thing as String[] and often leads to casts or runtime issues.

Summary

  • A Java varargs parameter is really an array, not a list.
  • You cannot pass an ArrayList directly to a T... parameter.
  • Convert the list with list.toArray(new String[0]) before calling the method.
  • Consider changing the API to accept List<T> when callers already have collections.
  • Be especially careful with generic varargs and raw toArray() results.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.