当前位置: 首页 > 知识库问答 >
问题:

LinkedList类无法访问其他类

邢杰
2023-03-14

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

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

}

共有1个答案

游皓
2023-03-14
private static class Node<Student> {

这相当于比较传统的

private static class Node<T> {

它声明了一个泛型节点类,带有一个类型参数T,可以是任何类型。所以

private Student data;

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

您可能希望Node和LinkedList类完全不是泛型的,因为它只应该包含学生,或者声明为

private static class Node<T extends Student> {
 类似资料:
  • 问题内容: 我的班级有一个名为DataStorage的哈希图: 如何在另一个类中访问此HashMap中的数据? 问题答案: 将HashMap创建为实例变量,并提供一种将其访问类API的方法:

  • 问题内容: 假设我正在构建一个井字游戏(因为它与结构非常相似),我希望结果在弹出窗口中显示,并带有一个新的游戏按钮,并且希望此弹出窗口允许我访问设置(另一个按钮)并对其进行更改,使其始终位于弹出窗口中,然后离开并最终将其关闭并开始新游戏。 我希望我可以保持秩序,因此有一个单独的弹出窗口类,可以在其中构建自定义弹出窗口。 显而易见,我将newgame方法和reset方法作为我的游戏网格类的方法。另一

  • 问题内容: 我最近拾起Java并遇到了问题。我有几个具有不同类的文件,但是我无法弄清楚如何访问文件中声明了其他类之外的其他类的对象。例如: 我不确定如何从其他文件和类本身有效访问其他类的这些对象?我知道我可以使对象静态化,然后通过它们所在的类将它们作为变量进行访问,但这似乎有点违反直觉?我来自面向对象较少的编程背景,因此我仍在尝试了解Java的编程风格。 问题答案: 您可能只想要这样的东西: 希望

  • 我在JQuery mobile和Eclipse中的PhoneGap中编写了一个android移动应用程序。在这个应用程序中,我调用jquery ajax来加载来自其他域的数据列表。 我的jQuery ajax调用代码是: 变量“WEBSERVICE\u URL”具有其他域php服务URL。在“pageview”事件上执行上述代码时,我遇到以下错误 有任何解决方案可以从JQuery Mobile P

  • 我的 Mac 可以访问 k8s 仪表板,但其他电脑无法访问。原因何在? @kubernetes/UI @kubernetes/仪表板 我已尝试使用最新版本的频道(稳定版或边缘版) 苹果操作系统版本: 10.14 适用于Mac的泊坞窗: 版本: 19.03.1 k8s 版本 : 1.14.3 eneble k8s on docker for mac 设置 apply k8s dashboard.ya

  • 1.系统版权是如何设置的? 打开【后台-店铺-店铺装修-版权设置】,此项功能针对授权用户。 2.seo关键词设置了,在百度上site还是旧的商城关键词怎么解决? 关键字最好固定,百度会根据抓取机制,(百度)刷新就会显示了。