为什么 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 有自己的函数可以转换为数组,但是数组需要一些辅助函数来转换为 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 实例是一个实际的对象,而数组(对于大多数意图和目的)是一个原语并且不公开方法.虽然从技术上讲,数组是一个对象,因此它们可以具有字段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屋!