无法添加新元素(节点)到ArrayList
Node N = new Node(5,"Sandeep");
Node N1 = new Node(5,"qwert");
在下面的行中我收到空指针异常
N.children.add(N1)
代码:
class Node {
public int val;
public String data;
public ArrayList<Node> children;
public Node(int val, String data) {
this.val = val;
this.data = data;
ArrayList<Node> children = new ArrayList<Node>();
}
}
public class Nary {
public static void main(String[] args) {
// ArrayList<Node> children = new ArrayList<Node>();
Node N = new Node(5,"Sandeep");
Node N1 = new Node(5,"qwert");
N.children.add(N1);
}
}
holdtom
catspeake
相关分类