我正在做一项作业,我必须从头开始创建一个链接列表,并且在编译时遇到错误“类 Node 中的构造函数 Node 无法应用于给定类型;”
这就是我正在尝试的,错误提示:
必需:无参数 发现:字符串
但是我看不出哪里出了问题,因为我的 Node 构造函数需要一个字符串?
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);
}
最佳答案
这个:
public void Node(String x) {
data = x;
next = null;
}
应该是:
public Node(String x) {
data = x;
next = null;
}
目前您有一个默认构造函数(不带参数),它是在没有任何显式构造函数的情况下隐式定义的。
关于Java 'constructor in class cannot be applied to given types' 'required: no arguments found: String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43090413/