如何初始化静态 List

发布于 2024-10-21 20:48:41 字数 357 浏览 1 评论 0原文

我似乎无法理解这个声明出了什么问题

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

第几種人 2024-10-28 20:48:41

列表是一种抽象类型,由各种类型的列表扩展和实现。
请尝试以下操作:

    public static void main ( String [] arg ) throws IOException {
         vertices = new ArrayList<Vertex>(); 
    }

List is an abstract type that is extended and implemented by various types of lists.
Try the following:

    public static void main ( String [] arg ) throws IOException {
         vertices = new ArrayList<Vertex>(); 
    }
方圜几里 2024-10-28 20:48:41

List是一个接口,不能实例化。请改用 ArrayList 或 LinkedList。

vertices = new ArrayList<Vertex>();

List is an interface and can not be instantinated. Use ArrayList or LinkedList instead.

vertices = new ArrayList<Vertex>();
美人如玉 2024-10-28 20:48:41

列表是一个接口。您需要使用一个实现 List 的类,例如 ArrayList。

A List is an interface. You need to use a class that implements List such as ArrayList.

不离久伴 2024-10-28 20:48:41

尝试一下:

vertices = new ArrayList<Vertex>();

List是Java中的一个接口,因此您需要使用它的实现之一。

http://download.oracle.com/javase/6 /docs/api/java/util/List.html

Try:

vertices = new ArrayList<Vertex>();

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

热情消退 2024-10-28 20:48:41

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 instantiate ArrayList or another implementation.

您需要使用 List 的实现,例如:

vertices = new ArrayList<Vertex>();

You need to use an implementation of a List, such as:

vertices = new ArrayList<Vertex>();
放肆 2024-10-28 20:48:41

Eclipse 抱怨因为 List 无法实例化,因为它是一个接口而不是一个具体类。
您在这里有 2 个选择 -

选项1:

public static List<Vertex> vertices; 

// where Vertex is a class with a default constructor 

public static void main ( String [] arg ) throws IOException {
vertices = new ArrayList<Vertex>(); // eclipse does not complain
}

选项2:

public static List<Vertex> vertices=new ArrayList<Vertex>(); 

// where Vertex is a class with a default constructor 

public static void main ( String [] arg ) throws IOException {
v
}

Eclipse complains because List can't be instantiated because it is an interface and not a concrete class.
You have 2 options here-

Option1:

public static List<Vertex> vertices; 

// where Vertex is a class with a default constructor 

public static void main ( String [] arg ) throws IOException {
vertices = new ArrayList<Vertex>(); // eclipse does not complain
}

Option2:

public static List<Vertex> vertices=new ArrayList<Vertex>(); 

// where Vertex is a class with a default constructor 

public static void main ( String [] arg ) throws IOException {
v
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文