开发者

Java : A List of type interface

开发者 https://www.devze.com 2023-04-11 04:13 出处:网络
I am trying to use the following code: List<ItemInterface> interfaceList = new List开发者_运维问答<ItemInterface>();

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

精彩评论

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

关注公众号