简体   繁体   中英

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.

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.

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 ...

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...

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 eg Kotlin, a language that compiles to the exact same bytecode as Java. Its arrays are full-featured objects.

Arrays are primitives which don't have methods(With the exception of String) so you have to use methods from another class. A list is a reference type so it can have methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM