在 Java 中从节点创建边

发布于 2024-11-10 06:55:49 字数 1633 浏览 5 评论 0原文

每当我尝试从节点创建简单边时,都会收到错误。 基本上,我创建了两个自己的类,称为 Node 和 Edge。

Node 类如下:

public class Node {

    public String ident;
    public int numLinks;

    public Edge[] neighbours;


    public Node (String ident) {
        this.ident = ident;

    }

    public void setNeighbour (Node start, Node end, int cost, int portNum) {

    }
}

我的 Edge 类如下:

public class Edge {

   Node start;
   Node end;
   int cost;
   int portNum;

   public Edge (Node a, Node b, int cost, int portNum) {
       this.start = a;
       this.end = b;
       this.cost = cost;
       this.portNum = portNum;
   }
}

在我的主类中,我创建两个节点,即开始节点和结束节点。我从文本文件中读取成本和端口号(这两个节点侦听的端口号)并将它们保存到名为“linkCostList”和“portNumList”的数组列表中。

现在,由于每个起始节点可以有多个边(我基本上是在创建一个图),因此我按以下方式调用 setNeighbour() 方法:

for (int i = 0; i < startNode.numLinks; i++) {
    nextNode = new Node (String name of node I read from text file)
    startNode.setNeighbour (startNode, nextNode, linkCostList.get(i), portNumList.get(i));
}

我的 setNeighbour 方法如下:

public void setNeighbour (Node start, Node end, int cost, int portNum) {
    for (int i = 0; i < start.numLinks; i++) {
        neighbours[i] = new Edge (start, end, cost, portNum);
    }
}

每当我编译它时,我都会得到以下类型的错误:

Exception in thread "main" java.lang.NullPointerException
    at Node.setNeighbour(Node.java: *line number*)
    at Start.startlsr(Start.java: *line number*)
    at graph.main(lsr.java: *line number*)
}

我知道这是一个空指针异常,因此,在该循环中的某个位置,我一定做错了什么。有人可以帮我解决一下吗?

I get an error whenever I try to create a simple edge from a node.
Basically, I've created two of my own classes called Node and Edge.

The Node class is as follows:

public class Node {

    public String ident;
    public int numLinks;

    public Edge[] neighbours;


    public Node (String ident) {
        this.ident = ident;

    }

    public void setNeighbour (Node start, Node end, int cost, int portNum) {

    }
}

And my Edge class is as follows:

public class Edge {

   Node start;
   Node end;
   int cost;
   int portNum;

   public Edge (Node a, Node b, int cost, int portNum) {
       this.start = a;
       this.end = b;
       this.cost = cost;
       this.portNum = portNum;
   }
}

In my main class, I create two nodes, namely, the start and the end node. The cost and the port number (port number that both these nodes listen to on), I read from a text file and am saving them into an array list named "linkCostList" and "portNumList".

Now, since each start node can have more than one edge (I'm basically creating a graph), I'm calling the setNeighbour() method in the following way:

for (int i = 0; i < startNode.numLinks; i++) {
    nextNode = new Node (String name of node I read from text file)
    startNode.setNeighbour (startNode, nextNode, linkCostList.get(i), portNumList.get(i));
}

My setNeighbour method is as follows:

public void setNeighbour (Node start, Node end, int cost, int portNum) {
    for (int i = 0; i < start.numLinks; i++) {
        neighbours[i] = new Edge (start, end, cost, portNum);
    }
}

Whenever I compile it, I get an error of the following sort:

Exception in thread "main" java.lang.NullPointerException
    at Node.setNeighbour(Node.java: *line number*)
    at Start.startlsr(Start.java: *line number*)
    at graph.main(lsr.java: *line number*)
}

I understand that this is a null pointer exception, so, somewhere in that loop, I must be doing something wrong. Could anyone please help me figure it out?

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

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

发布评论

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

评论(2

丶情人眼里出诗心の 2024-11-17 06:55:49

您是否已在 Node 类中初始化了 neighbours ?看起来异常是由于访问空数组 (neighbours[i]) 引起的。

另外,看起来 neighbours 数组会动态增长/收缩?在这种情况下,不要使用数组,而是考虑使用 ArrayList,这样您就不必自己增加邻居

Have you initialzed neighbours in your Node class? It looks like the exception is from accessing a null array (neighbours[i]).

Also It looks like the neighbours array will grow/shrink dynamically? In this case, instead of using array, consider using ArrayList so you don't have to grow neighbours yourself.

瑕疵 2024-11-17 06:55:49

除了@Alvin的回答(你没有初始化邻居,也没有允许扩展),试试这个:

public List<Edge> neighbours = new ArrayList<Edge>();

也利用java的“foreach”:

    for (Edge edge : start.neighbours) {
        // .. some code
    } 

考虑将“neighbours”重命名为“edges”,因为它们就是那个 - 的边缘节点。

最后,看来你的逻辑有问题。似乎太复杂了。您可能试图保留邻居的邻居的参考资料。考虑在需要时简单地查找内容。

Further to @Alvin's answer (you haven't initialized neighbours nor allowed for expansion), try this:

public List<Edge> neighbours = new ArrayList<Edge>();

Also make use of java's "foreach":

    for (Edge edge : start.neighbours) {
        // .. some code
    } 

Consider renaming "neighbours" to "edges", since they are just that - the edges of the node.

Finally, it seems you have a bug in your logic. It seems too complicated. You may be attempting to keep references of neighbours of neighbours. Consider simply looking stuff up when you need it.

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