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.
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:
This works:
But this does not:
That fails because Java sees an ArrayList<String>, not a String[].
Convert the List to an Array
The standard solution is toArray:
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:
into something conceptually similar to:
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:
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:
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
ArrayListdirectly to aT...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
- How to pass JVM options from bootRun
- How to pass multiple bootstrap servers for listener using spring-kafka
- How to pass system property to Gradle task
- How to pass TTL in Cassandra Java Driver QueryBuilder?
- How to pause / sleep thread or process in Android?
- how to pause and resume @KafkaListener using spring-kafka
- How to perform an action only if the Mono is empty and throw an error if not empty
- How to persist a property of type ListString in JPA?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.