Java 'constructor in class cannot be applied to given types' 'required: no arguments found: String'

标签 java constructor compiler-errors

我正在做一项作业,我必须从头开始创建一个链接列表,并且在编译时遇到错误“类 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);
    }

This is a screenshot of the error the compiler is showing

最佳答案

这个:

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/

相关文章:

java - 主线程等待

java - JPA:根据实例变量动态地将实体映射到表

java - 如果单例类构造函数是私有(private)的并且从其他类调用方法*参见代码*那么它是如何工作的

JavaScript 库模板

java - 如何使用我的所有方法修复“表达式的非法开头”?

typescript - 如何在 TypeScript 中将导入的模块分配给具有索引签名的字典类型?

java - 在插入表之前清理数据

javascript - 如何使用 call() 进行继承。错误 : Class constructor cannot be invoked without 'new'

c++ - T::T(T&)有什么用?

java - block 作用域变量