java - 访问作为值存储在 HashMap 中的节点

标签 java collections tree

我的节点有节点列表(作为子节点)。 ref.get(L.get(t)).children.add(N); L.get(t)正在给我的父节点。 ref.get(L.get(t))是我想要使用的另一个节点。 我想添加我的新节点 Nref.get(L.get(t)) 的 child 。调试时我可以在 ref.get(L.get(t)) 中看到一个节点但扔NullPointerException .

public void CreateNode(Node m,ArrayList<Node> S,HashMap<Node,Node> ref,ArrayList<String> L) {   
    Node N = new Node(m.val,m.data);
    ref.put(m,N);

    if(S.isEmpty()) {
        S.add(N);
    } else {
        Node c =ref.get(L.get(t));          //Showing C as null
        ref.get(L.get(t)).children.add(N);  //unable to access node in ref.get(L.get(t))
        System.out.println(ref.get(L.get(t)).val); 
        S.add(N);
        t++;
    }
}

最佳答案

关于您的方法签名,L 是一个 String 列表,因此 L.get(int) 将返回一个 String。另一方面,ref Map 的键是 Node 对象,因此使用 String 进行查找将返回 null。 因此,可以重构您的 ref Map 以具有 Node 的唯一字符串标识符,也可以通过 Node 对象进行查找。

关于java - 访问作为值存储在 HashMap 中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535192/

相关文章:

java - 更新 JavaFX 标签不适用于数据绑定(bind)

java assertEquals 集合

java - 如何初始化和访问集合对象数组

kotlin - Kotlin-基于大小和时间的 block 序列

php - 使用 PHP 从 SQL 表构建 html 列表树

java - 在 View 上使用线程和绘图 Canvas 的空指针,android 初学者,包括 logcat

Java 数据结构与 Redis

c# - 用于创建有序集合的 Order By 与 Sort

c++ - 二叉树的最小高度?

c# - 如何加 express 归搜索功能?