开发者

Java : A List of type interface

I am trying to use the following code:

List<ItemInterface> interfaceList = new List开发者_运维问答<ItemInterface>();

Eclipse gives an error: Cannot instantiate the type List

What is wrong with this code?


List is an Interface itself. You'll need to use something like:

List<ItemInterface> interfaceList = new ArrayList<ItemInterface>(); 


List<T> is an interface, not a class.

You probably mean new ArrayList<T>().


List<T> is an interface in Java, you have to use one of the classes that implement List.

Here's a lesson/tutorial from the Java tutorial: http://download.oracle.com/javase/tutorial/collections/interfaces/

Here's the List java documentation, with the list of classes in the Java API that implement it: http://download.oracle.com/javase/6/docs/api/java/util/List.html

For you, you probably want to use ArrayList<T>, so:

import java.util.ArrayList;
...
List<ItemInterface> interfaceList = new ArrayList<ItemInterface>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜