gpt4 book ai didi

java - 链表和对象问题

转载 作者:行者123 更新时间:2023-11-30 07:53:46 25 4
gpt4 key购买 nike

我正在尝试使用对象实现链接列表。当我编译代码时收到此错误消息:

Person.java:49: error: constructor Node in class Node cannot be applied to given types;
Node newNode = new Node(last, first, age);

有人可以帮我一下吗?为什么会发生这种情况?谢谢。这是代码:

    class Person{

private String lastName;
private String firstName;
private int age;

public Person(String last, String first, int a){
lastName=last;
firstName=first;
age=a;
}

public void displayPerson(){
System.out.println("Last Name: "+lastName);
System.out.println("First name"+firstName);
System.out.println("Age: "+age);
}

public String getLast(){
return lastName;
}
}

class Node
{
public Person data;
public Node next;

public Node(Person d)
{
data = d;
}

}
class LinkList
{
private Node first;

public LinkList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(String last, String first, int age)
{
Node newNode = new Node(last, first, age);
newNode.next = first;
first = newNode;
}
public Node deleteFirst(String last, String first, int age)
{
Node temp = first;
first = first.next;
return temp;
}
public void displayList()
{
System.out.print("Linked List (first -->last): ");
Node current = first;
while(current != null)
{
current.displayPerson();
current = current.next;
}
System.out.println(" ");
}
}

最佳答案

线路

Node newNode = new Node(last, first, age);

无法编译,因为 Node 类没有具有这些类型的三个参数的构造函数。看来你想要

Node newNode = new Node(new Person(last, first, age));

关于java - 链表和对象问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32977147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com