邻接列表 HashMap>无法找到其值

发布于 2024-10-11 23:41:46 字数 2226 浏览 5 评论 0 原文

我正在使用图形数据的邻接列表表示来调试广度优先搜索算法:HashMap>。每个 String 键都是一个地铁站的名称,每个 ArrayList 是该站的边列表。

我使用队列按照遍历的顺序存储图形的节点。所以我检查队列中的下一个是否是孩子的名字。然后,我想要通过使用类似 childEdges =stationAdjacencyList.get(childNodeName); 的方式从 adjacencyList 获取子项的边 ArrayList。

我的语法有点不同,但请检查下面的代码。

目前 .get() 函数不返回 ArrayList,而是每次都返回 null。我知道 HashMap 查找正在接收正确的密钥。它只是拒绝从其关联的存储桶中向我提供任何价值。

    while (!q.empty()) {    // 

        String endpointName; // the Key part for the next node lookup 

        // get next node (single entry of adjacency list)
        Map<String, ArrayList<Edge>> currentNode = (Map<String, ArrayList<Edge>>) q.deque(); 

        HashMap<String, ArrayList<Edge>> nextNode = new HashMap<String, ArrayList<Edge>>();

        for (Map.Entry<String, ArrayList<Edge>> node : currentNode.entrySet()) { // there is only one node

            ++levelCount; // next node iteration is one level down the tree

            for (Edge edge : node.getValue()) {  // for each of this nodes Edges

                endpointName = edge.getEndpoint(); // retrieve the name of adjacent

                if (!endpointName.equals(destination)) { // if it's not the destination



                    levelTracker.put(edge.getParent(), levelCount); // record the level in the tree of this node

                    ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName);

                    nextNode.put(endpointName, nextNodeEdges); // create child node from endpoint

                    q.enqueue(nextNode); // add child to queue

                }
                else if (endpointName.equals(destination)) { // if we're done

                    path.add(endpointName); // record the destination in the path (reverse order)

                    getPathBack(edge, levelCount + 1); // + 1 levelCount to indicate destination level in tree 

                    break;
                }
            }
        }

    }

如果代码不那么干净或没有像样的注释,我们深表歉意,它会不断变化。希望有人能告诉我为什么 ArrayList 会这样。 nextNodeEdges = adjacencyList.get(endpointName); 未获取任何内容。

谢谢!!

I am halfway through debugging a Breadth-First Search algorithm using an adjacency list representation of graph data: HashMap<String, ArrayList<Edge>>. Each String key is the name of an underground station, and each ArrayList is a list of the edges for that station.

I am using a queue to store nodes of the graph in the order they are traversed. So I inspect the next in the queue for it's child's name. I then want to get the child's ArrayList of edges from the adjacencyList by using something like childEdges = stationsAdjacencyList.get(childNodeName);.

My syntax is a little different, but please check the code below.

At the moment the .get() function is not returning an ArrayList but is returning null every time instead. I know that the HashMap lookup is receiving the correct Key. It is just refusing to give me any value from it's associated bucket.

    while (!q.empty()) {    // 

        String endpointName; // the Key part for the next node lookup 

        // get next node (single entry of adjacency list)
        Map<String, ArrayList<Edge>> currentNode = (Map<String, ArrayList<Edge>>) q.deque(); 

        HashMap<String, ArrayList<Edge>> nextNode = new HashMap<String, ArrayList<Edge>>();

        for (Map.Entry<String, ArrayList<Edge>> node : currentNode.entrySet()) { // there is only one node

            ++levelCount; // next node iteration is one level down the tree

            for (Edge edge : node.getValue()) {  // for each of this nodes Edges

                endpointName = edge.getEndpoint(); // retrieve the name of adjacent

                if (!endpointName.equals(destination)) { // if it's not the destination



                    levelTracker.put(edge.getParent(), levelCount); // record the level in the tree of this node

                    ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName);

                    nextNode.put(endpointName, nextNodeEdges); // create child node from endpoint

                    q.enqueue(nextNode); // add child to queue

                }
                else if (endpointName.equals(destination)) { // if we're done

                    path.add(endpointName); // record the destination in the path (reverse order)

                    getPathBack(edge, levelCount + 1); // + 1 levelCount to indicate destination level in tree 

                    break;
                }
            }
        }

    }

Apologies if the code is not so clean or with decent comments, it is changing constantly. Hopefully someone can tell me why the ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName); is not fetching anything.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-10-18 23:41:46

因此,一个好的测试是查看在同一位置使用硬编码值调用 adjacencyList.get("valid endpoint"); 是否会返回非空列表。如果没有,那么 adjacencyList 就会在某个地方被破坏,如果是,那么 endpointName 并不像您想象的那么正确。

So a good test is to see if calling adjacencyList.get("valid endpoint"); in the same spot with a hardcoded value will return a non-null list. If it doesn't then adjacencyList is getting destroyed somewhere, if it does then endpointName isn't as correct as you think it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文