我创建了一个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);
还有另外一个类:
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
。它指的是一个特定的实例,不为空。
关于java - java中添加到arrayList时,arrayList中的所有元素均为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35071342/