"实例" Java中的列表? [英] "Instantiating" a List in Java?

查看:1864
本文介绍了"实例" 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.

这篇关于&QUOT;实例&QUOT; Java中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆