Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java's List have "List.toArray()", but arrays don't have "Array.toList()"?

Arrays don't have a "toList" function, so we need "Arrays.asList" helper functions to do the conversion.

This is quite odd: List has its own function to convert to an array, but arrays need some helper functions to convert to a List. Why not let arrays have a "toList" function, and what's the reason behind this Java design?

Thanks a lot.

like image 955
Hind Forsum Avatar asked Dec 24 '18 02:12

Hind Forsum


People also ask

What is the use of toArray () in Java?

The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.

Does arrays asList create immutable list?

Arrays. asList() , the list is immutable.

What is the difference between list of and arrays asList?

Conclusion time: use List. of when you want a list that doesn't change and Arrays. asList when you want a list that can change (as shown above).

How do you assign an array to a list in Java?

Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class. Collections. addAll() method - Create a new list before using this method and then add array elements using this method to existing list.


4 Answers

Because List instances are an actual object, while arrays are (for MOST intents and purposes) a primitive and don’t expose methods. Although technically arrays are an object which is how they can have a field length and a method call such as clone(), but their classes are created after compilation by the JVM.

like image 50
Sebastiaan van den Broek Avatar answered Oct 22 '22 18:10

Sebastiaan van den Broek


Another point to consider is that toArray() is declared on an INTERFACE (specifically, the java.util.List interface).

Consequently, you must call toArray() on some object instance that implements List.

This might help explain at least one part of your question: why it's not static ...

like image 33
paulsm4 Avatar answered Oct 22 '22 17:10

paulsm4


As others have pointed out: An array in Java is a rather "low-level" construct. Although it is an Object, it is not really part the object-oriented world. This is true for arrays of references, like String[], but even more so for primitive arrays like int[] or float[]. These arrays are rather "raw, contiguous blocks of memory", that have a direct representation inside the Java Virtual Machine.

From the perspective of language design, arrays in Java might even be considered as a tribute to C and C++. There are languages that are purely object-oriented and where plain arrays or primitive types do not exist, but Java is not one of them.

More specifically focusing on your question:

An important point here is that Arrays#asList does not do any conversion. It only creates List that is a view on the array. In contrast to that, the Collection#toArray method indeed creates a new array. I wrote a few words about the implications of this difference in this answer about the lists that are created with Arrays#asList.

Additionally, the Arrays#asList method only works for arrays where the elements are a reference type (like String[]). It does not work for primitive arrays like long[] *. But fortunately, there are several methods to cope with that, and I mentioned a few of them in this answer about how to convert primitive arrays into List objects.


* In fact, Arrays#asList does work for primitive arrays like long[], but it does not create a List<Long>, but a List<long[]>. That's often a source of confusion...

like image 28
Marco13 Avatar answered Oct 22 '22 16:10

Marco13


From the modern perspective, there's no good reason why arrays lack the method you mention as well as a ton of other methods, which would make them so much easier to use. The only objective reason is that "it made the most sense to the designers of Java twenty two years ago". It was a different landscape in 1996, an era where C and C++ were the dominant general purpose programming languages.

You can compare this to e.g. Kotlin, a language that compiles to the exact same bytecode as Java. Its arrays are full-featured objects.

like image 39
Marko Topolnik Avatar answered Oct 22 '22 16:10

Marko Topolnik