"实例" Java中的列表?
尝试使用以下代码:
List<Integer> list = new List<Integer>();
我收到以下错误消息:
java.util.List
是抽象的;无法实例化
java.util.List
is abstract; cannot be instantiated
这是什么意思,为什么我不能初始化列表
和我的 ArrayList
一样?
What does this mean and why can't I initialize a List
the same way I would an ArrayList
?
在Java中,列表
是一个界面。也就是说,它无法直接实例化。
In Java, List
is an interface. That is, it cannot be instantiated directly.
相反,你可以使用 ArrayList
这是该接口的一个实现。使用数组作为其后备存储(因此名称)。
Instead you can use ArrayList
which is an implementation of that interface that uses an array as its backing store (hence the name).
由于 ArrayList
是一种列表
,你可以轻松地将其翻译:
Since ArrayList
is a kind of List
, you can easily upcast it:
List<T> mylist = new ArrayList<T>();
这与.NET形成鲜明对比,从版本2.0开始, List< ; T>
是 IList< T>
界面的默认实现。
This is in contrast with .NET, where, since version 2.0, List<T>
is the default implementation of the IList<T>
interface.