如何初始化静态 List?
我似乎无法理解这个声明出了什么问题
public static List<Vertex> vertices;
// where Vertex is a class with a default constructor
public static void main ( String [] arg ) throws IOException {
vertices = new List<Vertex>(); // eclipse complains
}
我应该在哪里以及如何初始化这个列表...... 因此,当我继续添加到列表中时,它抱怨空指针异常......任何人都可以告诉我我做错了什么......
I can't seem to understand what is going wrong in this declaration
public static List<Vertex> vertices;
// where Vertex is a class with a default constructor
public static void main ( String [] arg ) throws IOException {
vertices = new List<Vertex>(); // eclipse complains
}
Where and how should i initialize this list.....
Due to this when I go on to add in the list it complains of null pointer exception..... Can anybody tell me what am i doing wrong....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
列表是一种抽象类型,由各种类型的列表扩展和实现。
请尝试以下操作:
List is an abstract type that is extended and implemented by various types of lists.
Try the following:
List是一个接口,不能实例化。请改用 ArrayList 或 LinkedList。
List is an interface and can not be instantinated. Use ArrayList or LinkedList instead.
列表是一个接口。您需要使用一个实现 List 的类,例如 ArrayList。
A List is an interface. You need to use a class that implements List such as ArrayList.
尝试一下:
List
是Java中的一个接口,因此您需要使用它的实现之一。http://download.oracle.com/javase/6 /docs/api/java/util/List.html
Try:
List
is an interface in Java, so you need to use one of its implementations.http://download.oracle.com/javase/6/docs/api/java/util/List.html
List
不是一个类,而是一个接口。由于接口不是任何可以实例化的事物的完整具体实现。您只能对抽象类进行 new 操作。因此,请尝试实例化 ArrayList 或其他实现。List
is no a class, but interface. As interface is not a full concrete implementation of anything it can be instantiated. You can do new only to not abstract classes. So try to instantiateArrayList
or another implementation.您需要使用 List 的实现,例如:
You need to use an implementation of a List, such as:
Eclipse 抱怨因为 List 无法实例化,因为它是一个接口而不是一个具体类。
您在这里有 2 个选择 -
选项1:
选项2:
Eclipse complains because List can't be instantiated because it is an interface and not a concrete class.
You have 2 options here-
Option1:
Option2: