2016-10-01 84 views
0

我有一個LinkedList類,由於某種原因無法訪問我的學生類方法。我不明白爲什麼會發生這種情況,因爲我的鏈表類是Student類型的。我繼續收到錯誤找不到符號符號:method getName()位置:類型爲Student的變量數據,其中Student是類型變量:Student extends在類節點中聲明的對象。下面的方法是從我的鏈表類LinkedList類不能訪問其他類

public double findGpa(String name){ 
    Node<Student> temp = head; 
    double foundData = 0.0; 
    for(int i = 1 ; (i < size); i++){ 
     if(temp.data.getName().equals(name)){ 
      foundData = temp.data.getGpa(); 
     } 
     temp = getNode(i); 
    } 

    return foundData; 
} 

getGpa在我的學生類中的方法,因爲節點的類型是學生和數據也是類學生,我不明白爲什麼它不能訪問學生的方法 我學生類如下

package lab3; 


public class Student { 

    private String fullName; 
    private double gpa; 

    public Student(String name, double gpa){ 
     fullName = name; 
     this.gpa = gpa; 
    } 

    public void setGpa(double grade){ 
     gpa = grade; 
    } 

    public void setName(String name){ 
     fullName = name; 
    } 

    public double getGpa(){ 
     return gpa; 
    } 

    public String getName(){ 
     return fullName; 
    } 

} 

這裏列出的是具有節點類作爲內部類全體LinkedList類

package lab3; 

/** 
* 
* @author Chris 
*/ 
/** 
* SingleLinkedList is a class that provides some of the 
* capabilities required by the List interface using 
* a single linked list data structure. 
* Only the following methods are provided: 
* get, set, add, remove, size, toString 
* @author Koffman and Wolfgang 
* @param <Student> 
*/ 
public class StudentSingleLinkedList<Student> { 

    private static class Node<Student> { 

     /** The data value. */ 
     private Node<Student> next; 
     /** The link */ 
     private Student data; 

     /** 
     * Construct a node with the given data value and link 
     * @param data - The data value 
     * @param next - The link 
     */ 
     public Node(Student d, Node<Student> next) { 
      this.next = next; 
      data = d; 
     } 

     /** 
     * Construct a node with the given data value 
     * @param data - The data value 
     */ 
     public Node(Student d) { 
      data = d; 
      next = null; 
     } 
    } 

    // Data fields 
    /** A reference to the head of the list */ 
    private Node<Student> head = null; 
    /** The size of the list */ 
    private int size = 0; 


    // Helper Methods 
    /** Insert an item as the first item of the list. 
    * @param item The item to be inserted 
    */ 
    private void addLast(Student item){ 

     Node<Student> newNode = getNode(size); 
     newNode.next = new Node<Student>(item, newNode.next); 
     size++; 

    } 

    private void addFirst(Student item) { 
     head = new Node<Student>(item, head); 
     size++; 
    } 

    /** 
    * Add a node after a given node 
    * @param node The node which the new item is inserted after 
    * @param item The item to insert 
    */ 
    private void addAfter(Node<Student> node, Student item) { 
     node.next = new Node<Student>(item, node.next); 
     size++; 
    } 

    /** 
    * Remove the first node from the list 
    * @returns The removed node's data or null if the list is empty 
    */ 
    private Student removeFirst() { 
     Node<Student> temp = head; 
     if (head != null) { 
      head = head.next; 
     } 
     if (temp != null) { 
      size--; 
      return temp.data; 
     } else { 
      return null; 
     } 
    } 

    /* 
    public double findGpa(String name){ 
     Node<Student 
    } 
    */ 


    private double findGpa(String name){ 
     Node<Student> temp = head; 
     double foundData = 0.0; 
     for(int i = 1 ; (i < size); i++){ 
      if(temp.data.getName().equals(name)){ 
       foundData = temp.data.getGpa(); 
      } 
      temp = getNode(i); 
     } 

     return foundData; 
    } 


    public Student updateGpa(String name){ 
     Node<Student> temp = head; 
     if(temp.next.equals(name)){ 
      temp.data = 
     } 
    } 
    /** 
    * Remove the node after a given node 
    * @param node The node before the one to be removed 
    * @returns The data from the removed node, or null 
    *   if there is no node to remove 
    */ 
    private Student removeAfter(Node<Student> node) { 
     Node<Student> temp = node.next; 
     if (temp != null) { 
      node.next = temp.next; 
      size--; 
      return temp.data; 
     } else { 
      return null; 
     } 
    } 

    /** 
    * Find the node at a specified index 
    * @param index The index of the node sought 
    * @returns The node at index or null if it does not exist 
    * 
    * 
    */ 
    private Node<Student> getNode(int index){ 
     Node<Student> temp = head; 
     if(temp == null){ 
      return null; 
     } 
     else{ 
      for(int i = 0; i < index - 1; i++){ 
       temp = temp.next; 
      }   
     } 
     return temp; 
    } 

    // Public Methods 
    /** 
    * Get the data value at index 
    * @param index The index of the element to return 
    * @returns The data at index 
    * @throws IndexOutOfBoundsException if the index is out of range 
    * 
    * 
    * Uses getNode() to access the nodes index then calls the .data to get 
    * whatever data is being stored inside that node. 
    */ 
    public Student get(int index) { 
     Node<Student> temp = getNode(index); 
     if(temp== null){ 
      throw new IndexOutOfBoundsException(); 
     } 

     return temp.data; 

    } 

    /** 
    * Set the data value at index 
    * @param index The index of the item to change 
    * @param newValue The new value 
    * @returns The data value previously at index 
    * @throws IndexOutOfBoundsException if the index is out of   
    * range 
    * 
    * 
    * Uses the getNode method to get the index of the node and replace it with 
    * the data that temp holds 
    */ 
    public Student set(int index, Student newValue) { 
     if(head == null){ 
      return null; 
     } 
     if(index > size){ 
      throw new IndexOutOfBoundsException(); 
     } 
     Node<Student> temp = getNode(index); 
     Student temp2 = temp.data; 
     temp.data = newValue; 
     return temp2; 

    } 

    /** 
    * Insert the specified item at the specified position in the list. 
    * Shifts the element currently at that position (if any) and any 
    * subsequent elements to the right (adds one to their indicies) 
    * @param index Index at which the specified item is to be inserted 
    * @param item The item to be inserted 
    * @throws IndexOutOfBoundsException if the index is out of range 
    * 
    * 
    * 
    * If index is less than 0 and greater than the size throw an exception 
    * If index is equal to 0 then item will be the first node 
    /** 
    * Insert the specified item at the specified position in the list. 
    * Shifts the element currently at that position (if any) and any 
    * subsequent elements to the right (adds one to their indicies) 
    * @param index Index at which the specified item is to be inserted 
    * @param item The item to be inserted 
    * @throws IndexOutOfBoundsException if the index is out of range 
    * 
    * 
    * 
    * If index is less than 0 and greater than the size throw an exception 
    * If index is equal to 0 then item will be the first node 
    */ 


    public void add(int index, Student item) { 
     if(index < 0 || index > size){ 
      throw new IndexOutOfBoundsException(); 
     } 
     if(index ==0){ 
      addFirst(item); 
     } 
     Node<Student> temp = head; 
     for(int i= 0; i < index-1; i++){ 
      temp = temp.next; 
     } 
     temp.next = new Node<Student>(item,temp.next); 
     size++; 


    } 

    /** 
    * Append the specified item to the end of the list 
    * @param item The item to be appended 
    * @returns true (as specified by the Collection interface) 
    */ 
    /** 
    * if head is null then temp which holds item will be the first node. 
    * While temp next node is not equal to null temp is equal to the next node 
    * When it is equal to null the while loop will stop and a new node is created 
    * and added to the end of the list 
    * @param item 
    * @return 
    */ 
    public boolean add(Student item) { 
     Student temp = item; 
     if(head == null){ 
      addFirst(temp); 
      return true; 
     } 
     Node<Student> temp2 = head; 
     while(temp2.next != null){ 
      temp2 = temp2.next; 
     } 
     temp2.next = new Node<Student>(temp); 
     size++; 
     return true; 


    } 
     int size() { 
     return size; 
    } 

    /** 
    * Obtain a string representation of the list 
    * @return A String representation of the list 
    */ 
    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder("["); 
     Node p = head; 
     if (p != null) { 
      while (p.next != null) { 
       sb.append(p.data.toString()); 
       sb.append(" ==> "); 
       p = p.next; 
      } 
      sb.append(p.data.toString()); 
     } 
     sb.append("]"); 
     return sb.toString(); 
    } 

    /** 
    * Remove the first occurence of element item. 
    * @param item The item to be removed 
    * @return true if item is found and removed; otherwise, return false. 
    */ 
    public boolean remove(Student item) { 
     if (head == null) { 
      return false; 
     } 
     Node<Student> current = head; 
     if (item.equals(current.data)) { 
      removeFirst(); 
      return true; 
     } 
     while (current.next != null) { 
      if (item.equals(current.next.data)) { 
       removeAfter(current); 
       return true; 
      } 
      current = current.next; 
     } 
     return false; 
    } 

    // Nested Class 
    /** A Node is the building block for the SingleLinkedList */ 

} 
+0

分享您的堆棧跟蹤 –

+0

這將是有益的,如果你發佈更多的代碼,特別是Node類的代碼。如果您發佈了錯誤消息的全文(我認爲這是來自編譯器的錯誤消息?),這也會很有幫助。 – mangotang

+0

@ bart.s恐怕我不知道該怎麼做 – Chris

回答

0
private static class Node<Student> { 

這等同於更傳統的

private static class Node<T> { 

它聲明一個通用的節點類,與一類參數T,其可以是任何類型的。所以

private Student data; 

實際上聲明瞭一個泛型類型的字段,它可以是任何東西。

你可能希望節點和LinkedList類不能通用於所有的,因爲它是唯一應該包含學生,或宣佈爲

private static class Node<T extends Student> { 
+0

當我將頭部更改爲時,它修復了問題但打破了一切 – Chris

+0

這不僅僅是你應該改變的頭。同樣,你可能不希望這些類是泛型的,所以你應該將它們聲明爲「private static class Node」和「public class StudentSingleLinkedList」。 –

+0

因此,工作和擺脫了錯誤,但我想我不完全理解爲什麼這是正確的方式?任何見解? – Chris