LinkedList类不能访问其他类

问题描述:

我有一个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

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