Not possible in Java.
It is possible initialize the array with a specific size to avoid needing relocations when the list gets larger than the array available.
The options cited in reply linked in comment above and other responses here do not generate a real list, generates something that looks like a list, but it is immutable and can not do everything expected from a list. And it’s not something efficient, it’s just a facilitator.
If this fits, then one array solves your problem and it can be initialized as you wish. Carrying the other solutions or do not solve the problem mentioned or the problem can be solved in a much more elegant and performative way.
It is possible to create a method that creates a real list from a sequence of arguments, but it is also not efficient. It is actually very inefficient because there will be two copies, one to initialize the array which will be passed to the method and another to copy to the list.
In C# you can do this, but it’s only syntactic sugar. In C++ you can do it, in certain circumstances efficiently.
Related: What’s the difference between
Arrays.asList
andList.of
?– Renan Gomes
Renan, thanks for adding to the post, had no knowledge of these methods, will certainly help me to arrive at the solution I need for the problem related to the question.
– BrunnoFdc
But can you tell me if the methods of this question you mentioned are available in Java 8? Or is it just Java 9 itself?
– BrunnoFdc
List.of
is present only in Java 9.Arrays.asList
for the longest time: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html– Renan Gomes