LinkedList类无法访问其他类

时间:2016-10-01 14:58:55

标签: java linked-list

我有一个LinkedList类,由于某种原因无法访问我的Student类方法。我不明白为什么会发生这种情况,因为我的链表类型是Student类型。我一直收到错误找不到符号符号:方法getName()位置:类型为Student的变量数据,其中Student是一个类型变量:Student扩展在类节点中声明的Object。以下方法来自我的链表类

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是我的Student类中的一个方法,因为Node是student类型,数据也是student类型我不明白为什么它不能访问Student方法 我的学生班级列在下面

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类,它将Node Class作为内部类

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 */

}

1 个答案:

答案 0 :(得分:0)

private static class Node<Student> {

这相当于更传统的

private static class Node<T> {

它声明了一个通用的Node类,其类型参数为T,可以是任何类型。所以

private Student data;

实际上声明了泛型类型的字段,可以是任何字段。

你可能希望Node和LinkedList类根本不是通用的,因为它只应该包含学生,或者被声明为

private static class Node<T extends Student> {
相关问题