在 java 添加到 arrayList 时,arrayList 中的所有元素都为 null
All elements in arrayList is null when add to arrayList in java
我创建了一个 List 调用 nodes_ 并初始化为 ArrayList,
当我向 nodes_ 添加内容时,所有内容均为空。
有一段代码:
public class MyOocmdgGeneration1 {
private List<Node> nodes_ = new ArrayList<Node>();
public MyOocmdgGeneration1() {
findDependency();
}
private void findDependency() {
ClassNode c1 = new ClassNode();
FieldNode x = new FieldNode();
MethodNode c1Constructor = new MethodNode();
MethodNode m1 = new MethodNode();
nodes_.add(c1);
nodes_.add(x);
nodes_.add(c1Constructor);
nodes_.add(m1);
还有另外一块class:
public abstract class Node {
private String path;
private List<Node> callees;
private List<Node> callers;
private List<Node> children;
private Node parent;
private int id;
public Node() {
this.callees = new ArrayList<>();
this.callers = new ArrayList<>();
this.children = new ArrayList<>();
}
public Node(String path, List<Node> callees, List<Node> callers) {
this();
this.path = path;
this.callees = callees;
this.callers = callers;
this.children = new ArrayList<>();
}
public Node(int id, String path, Node parent) {
this();
this.id = id;
this.path = path;
this.parent = parent;
parent.addChild(this);
}
当我调试时,它看起来像这样:debug image
您看到的 ("null"
) 是对每个 类 调用的 toString()
的输出。它们实际上不是空的。
你怎么知道?调试器显示每个条目,如 ClassNode@464
。那指的是一个特定的实例,不是空的。
节点不为空。对象都在那里,但它们的 toString 表示是 "null".
我创建了一个 List 调用 nodes_ 并初始化为 ArrayList, 当我向 nodes_ 添加内容时,所有内容均为空。 有一段代码:
public class MyOocmdgGeneration1 {
private List<Node> nodes_ = new ArrayList<Node>();
public MyOocmdgGeneration1() {
findDependency();
}
private void findDependency() {
ClassNode c1 = new ClassNode();
FieldNode x = new FieldNode();
MethodNode c1Constructor = new MethodNode();
MethodNode m1 = new MethodNode();
nodes_.add(c1);
nodes_.add(x);
nodes_.add(c1Constructor);
nodes_.add(m1);
还有另外一块class:
public abstract class Node {
private String path;
private List<Node> callees;
private List<Node> callers;
private List<Node> children;
private Node parent;
private int id;
public Node() {
this.callees = new ArrayList<>();
this.callers = new ArrayList<>();
this.children = new ArrayList<>();
}
public Node(String path, List<Node> callees, List<Node> callers) {
this();
this.path = path;
this.callees = callees;
this.callers = callers;
this.children = new ArrayList<>();
}
public Node(int id, String path, Node parent) {
this();
this.id = id;
this.path = path;
this.parent = parent;
parent.addChild(this);
}
当我调试时,它看起来像这样:debug image
您看到的 ("null"
) 是对每个 类 调用的 toString()
的输出。它们实际上不是空的。
你怎么知道?调试器显示每个条目,如 ClassNode@464
。那指的是一个特定的实例,不是空的。
节点不为空。对象都在那里,但它们的 toString 表示是 "null".