为什么Java的List具有“List.toArray()”,但是数组没有“Array.toList()”? [英] Why does Java's List have "List.toArray()", but arrays don't have "Array.toList()"?
问题描述
数组没有toList函数,所以我们需要Arrays.asList辅助函数来进行转换。
Arrays don't have a "toList" function, so we need "Arrays.asList" helper functions to do the conversion.
这很奇怪:列表有自己的函数转换为数组,但数组需要一些辅助函数来转换为List。为什么不让数组有一个toList函数,这个Java设计背后的原因是什么?
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?
非常感谢。
推荐答案
因为List实例是一个实际的对象,而数组(对于MOST意图和目的)是一个原语,并且不公开方法。虽然技术上数组是一个对象,它是如何使用字段 length
和方法调用,如 clone()
,但它们的类是在JVM编译后创建的。
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.
这篇关于为什么Java的List具有“List.toArray()”,但是数组没有“Array.toList()”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!