如何创建List <Integer>类型的Object

Ian*_*son 0 java generics static

我在创建和使用对象输入时遇到问题List<Integer>.当我运行以下代码时,我得到一个NullPointerException因为Object没有初始化.

import java.util.List;

public class RBTree {

    public static class Tree {
        public List<Integer> parent;
        public List<Integer> right;
        public List<Integer> left;
        public List<Integer> data;
        public List<Boolean> black;
    }

    public static void main (String[] args){
        Tree rb =new Tree();                
        rb.data.add(-1);
        rb.left.add(-1);
        rb.right.add(-1);
        rb.parent.add(-1);
        rb.black.add(Boolean.TRUE);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器也给我错误,除非我添加staticpublic static class Tree行,但我不想Treestatic不可变的.我需要能够struct在C中使用或多或少的树.

Oli*_*rth 5

到目前为止,您只创建了一个引用,没有底层对象.请尝试以下方法:

public List<Integer> parent = new ArrayList<Integer>();
// etc.
Run Code Online (Sandbox Code Playgroud)