2015-10-06 74 views
1

我想用對象實現鏈表。當我編譯代碼時,我得到這個錯誤味精:鏈表和對象問題

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(" "); 
    } 
} 
+0

你在哪裏有一個節點構造函數,它需要'string,string,int'?另外'first = newNode;'是不明確的,我想你的意思是寫一些像'this.first = newNode;' – tnw

回答

1

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

不能編譯,因爲節點類不具有這些類型的三個參數的構造函數。它似乎想要

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

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

節點沒有一個構造函數接受3個參數。

我假設你的意思是先創建一個Person對象,然後傳遞到Node構造,如new Node(new Person(last, first, age))

0

我看到你的代碼中的一些其他錯誤,例如:

  • 在insertFirst方法中,您的String first參數隱藏您的Node first屬性。您應該重命名參數,以避免這種
  • 在您的顯示列表的方法喲有current.displayPerson(),我想你想current.data.displayPerson()

希望這有助於:) 阿爾貝託