.toArraynew MyClass0 or .toArraynew MyClassmyList.size?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Java, both list.toArray(new MyClass[0]) and list.toArray(new MyClass[list.size()]) are correct. They both return a properly typed array containing the list elements in order.
The real question is not correctness but style and performance. In modern Java, the practical difference is small enough that clarity usually matters more than micro-optimization.
What toArray(T[]) Actually Does
The generic overload toArray(T[] a) uses the runtime type of the array you pass in to create a correctly typed result. That is why you pass new MyClass[...] instead of calling the raw toArray() method, which returns Object[].
Example:
If the supplied array is too small, Java allocates a new array of the right type and size. If the supplied array is large enough, Java may reuse it.
Comparing the Two Common Forms
These are the two styles most developers debate:
Functionally, both produce the same content.
The historical argument for new MyClass[list.size()] was that it might avoid an extra allocation. The historical argument for new MyClass[0] was that it is shorter and avoids repeating the list variable in the expression.
In current Java practice, the difference is usually not important enough to drive design unless you have measured a real bottleneck.
Why Many Developers Prefer new MyClass[0]
The zero-length form became popular because it is compact and clearly communicates intent: “give me a typed array of this list.”
It also avoids coupling the call to the current list size in the source code. That makes the line easy to read and harder to get wrong when refactoring.
For most application code, this is perfectly reasonable and widely accepted.
When the Sized Array Form Is Fine
The sized form is also valid and readable:
Some teams prefer it because it makes the target size explicit. If your codebase consistently uses that style, it is not a problem.
The important point is not to invent performance folklore without measurement. Both forms are standard Java code.
A Newer Alternative
If you are using a stream, Java provides an even cleaner pattern with an array constructor reference:
That example is for streams, not List.toArray, but it shows the same principle: preserve the element type without falling back to Object[].
What Actually Matters More
The biggest real mistake is not choosing one array size or the other. It is using raw toArray() and then casting the result.
That loses type safety and often leads to awkward casts later. If you know the target type, use the generic overload.
Another thing that matters more than the size choice is consistency. A team that uses one style uniformly will have more readable code than a team that mixes three different styles based on hearsay.
Common Pitfalls
A common mistake is assuming new MyClass[list.size()] is always faster in a meaningful way. In most real code, that difference is too small to matter unless profiling proves otherwise.
Another mistake is using toArray() without a typed array and then casting the result to MyClass[]. That is unsafe and can fail at runtime.
Developers also sometimes cargo-cult whichever form they saw in an old benchmark without checking whether their current JDK and workload behave the same way.
Finally, do not optimize array-conversion style while ignoring much larger costs in the surrounding code such as I/O, database calls, or repeated list construction.
Summary
- Both
toArray(new MyClass[0])andtoArray(new MyClass[list.size()])are correct. - The practical performance difference is usually negligible in normal application code.
- Many developers prefer
new MyClass[0]because it is short and idiomatic. - The typed overload matters more than the exact array size argument.
- Use one clear style consistently unless profiling gives you a reason to do otherwise.
Related reading
- ''too many values to unpack'', iterating over a dict. keystring, valuelist
- Topological sort based on a comparator rather than a graph
- Topological sort, but with a certain kind of grouping
- Topological sort in OCaml
- Toilet Seat Algorithm
- Tomcat takes too much time to start - Java SecureRandom
- Tomcat How to find out running Tomcat version?
- Tomcat threads vs Java threads

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.