Java中如何通过构造函数连接两个StringNode节点?
Hey there, let's work through this problem step by step to get your nodes connected properly!
1. Why you're seeing the "Edgelist(StringNode, StringNode) is undefined" error
The core issue here is that you're trying to create an EdgeList instance using new EdgeList(node1, node2), but your EdgeList class doesn't have a constructor that accepts two StringNode parameters. Java automatically generates a no-argument constructor for classes unless you define your own, so the compiler can't find the constructor you're trying to use.
2. Fixing the constructor error + fixing your node connection logic
Your original insert method also has a critical flaw: it uses recursive logic with no termination condition, which would cause a StackOverflowError if it ran. Let's fix both issues with two clear options:
Option 1: Add a parameterized constructor to handle connection on instantiation
If you want to connect the nodes as soon as you create an EdgeList object, add a constructor that takes the two nodes and handles the connection:
package Linear; public class EdgeList { // New constructor that connects the two nodes public EdgeList(StringNode firstNode, StringNode secondNode) { // Connect firstNode to secondNode (adjust this logic if you need reverse connection) firstNode.next = secondNode; } // Optional: If you still need an insert method, here's a corrected version public StringNode insertToEnd(StringNode head, StringNode newNode) { if (head == null) { return newNode; } StringNode current = head; // Traverse to the end of the list while (current.next != null) { current = current.next; } current.next = newNode; return head; } }
Then call it like this in your main method:
public static void main(String[] args) { StringNode everest = new StringNode("Everest", null); StringNode kilimanjaro = new StringNode("Kilimanjaro", null); // Now this works because we defined the parameterized constructor EdgeList edgeList = new EdgeList(everest, kilimanjaro); // Test the connection System.out.println(everest.next); // Outputs "Kilimanjaro" }
Option 2: Use the default no-arg constructor + a dedicated connection method
If you prefer to separate instantiation and connection logic, keep the default constructor and create a clear method to link the nodes:
package Linear; public class EdgeList { // Default no-arg constructor (automatically provided, but we can write it explicitly) public EdgeList() {} // Simple method to connect two nodes public void connectNodes(StringNode sourceNode, StringNode targetNode) { sourceNode.next = targetNode; } }
Call it like this:
public static void main(String[] args) { StringNode everest = new StringNode("Everest", null); StringNode kilimanjaro = new StringNode("Kilimanjaro", null); // Use default constructor EdgeList edgeList = new EdgeList(); // Call the connection method edgeList.connectNodes(everest, kilimanjaro); // Verify the link System.out.println(everest.next); // Outputs "Kilimanjaro" }
3. Why your original insert method was broken
Your original insert method used secondNode.next = insert(firstNode, secondNode.next); which is an infinite recursive call—there's no condition to stop the recursion (like checking if secondNode.next is null). This would crash your program with a StackOverflowError as soon as it ran, so replacing that logic with a straightforward connection or a properly bounded traversal is essential.
内容的提问来源于stack exchange,提问作者CodingIsHardMan

