I'm working on an assignment where I have to create a linked list from scratch and have come across an error when compiling that the "constructor Node in class Node cannot be applied to given types;"
This is what I'm trying, the error says:
required: no arguments
found: string
Yet I cannot see where I am going wrong as my constructor for Node requires a string?
public class Node {
String data;
Node next;
public void Node(String x) {
data = x;
next = null;
}
}
public class stringList {
private Node head;
private int count;
public void stringList() {
head = null;
count = null;
}
public void add(String x) {
Node temp = new Node(x);
}
解决方案
This:
public void Node(String x) {
data = x;
next = null;
}
should be:
public Node(String x) {
data = x;
next = null;
}
Currently you have a default constructor (taking no arguments), which is implicitly defined in the absence of any explicit constructors.