Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 链表和对象问题_Java_Data Structures - Fatal编程技术网

Java 链表和对象问题

Java 链表和对象问题,java,data-structures,Java,Data Structures,我正在尝试使用对象实现一个链表。当我编译代码时,我得到这个错误消息: 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

我正在尝试使用对象实现一个链表。当我编译代码时,我得到这个错误消息:

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(" ");
   }
}
谁能帮我一把吗?为什么会这样?非常感谢。 代码如下:

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 newNode = new Node(new Person(last, first, age));

Node newNode=新节点(最后一个、第一个、年龄)

节点没有接受3个参数的构造函数


我假设您打算先创建一个
Person
对象,然后将其传递给
节点
构造函数,例如
新节点(new-Node(new-Person(last,first,age))

我在代码中看到一些其他错误,例如:

  • 在insertFirst方法中,String first参数隐藏了Node first属性。您应该重命名参数以避免这种情况
  • 在displayList方法中,您有current.displayPerson(),我想您需要current.data.displayPerson()
希望这有帮助:)
Alberto

哪里有一个节点构造函数,它接受
字符串、字符串、int
?同样
first=newNode
是不明确的,我想你的意思是写一些类似于this.first=newNode的东西