Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何初始化静态列表<;T>;?_Java_Collections_Static_Initialization - Fatal编程技术网

Java 如何初始化静态列表<;T>;?

Java 如何初始化静态列表<;T>;?,java,collections,static,initialization,Java,Collections,Static,Initialization,我似乎不明白这份声明出了什么问题 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 } 公共静态列表顶点

我似乎不明白这份声明出了什么问题

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
}
公共静态列表顶点;
//其中Vertex是具有默认构造函数的类
公共静态void main(字符串[]arg)引发IOException{
顶点=新列表();//eclipse抱怨
}
我应该在何处以及如何初始化此列表。。。。。
因此,当我继续添加到列表中时,它会抱怨空指针异常。。。。。有人能告诉我我做错了什么吗……

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

    public static void main ( String [] arg ) throws IOException {
         vertices = new ArrayList<Vertex>(); 
    }
publicstaticvoidmain(字符串[]arg)引发IOException{
顶点=新的ArrayList();
}

列表是一个界面。您需要使用实现列表的类,如ArrayList。

请尝试:

vertices = new ArrayList<Vertex>();
顶点=新的ArrayList();
List
是Java中的一个接口,因此需要使用它的一个实现


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

vertices = new ArrayList<Vertex>();
顶点=新的ArrayList();

列表
不是类,而是接口。因为接口不是任何东西的完整具体实现,所以它可以被实例化。您可以只对抽象类执行新操作。因此,尝试实例化
ArrayList
或其他实现。

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

vertices = new ArrayList<Vertex>();
顶点=新的ArrayList();

Eclipse抱怨,因为列表是接口而不是具体类,所以无法实例化。 你有两个选择-

选项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
}
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
}
公共静态列表顶点;
//其中Vertex是具有默认构造函数的类
公共静态void main(字符串[]arg)引发IOException{
顶点=新ArrayList();//eclipse不会抱怨
}
选项2:

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
}
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
}
公共静态列表顶点=新建ArrayList();
//其中Vertex是具有默认构造函数的类
公共静态void main(字符串[]arg)引发IOException{
v
}

它会出现以下错误“无法实例化类型列表”是否确实编译<代码>列表是一个接口,无法实例化。(必须说明可变静态是有害的。)