如何访问存储在单链表节点中的对象的特定变量(迭代)

How to access particular variable of object stored in node of singly linked list (iteration)

这是一个非常具体的问题,我似乎找不到任何特别有用的东西。我有一个单向链表(不是一个已实现的链表,这是我所能找到的),其中节点存储一个 Student object.Each Student 对象有变量,尽管我在访问每个变量时遇到问题.

我认为它应该类似于您如何遍历 objects.But 的数组,该数组使用 for loop.And 来遍历您必须使用节点及其节点的链表下一个&数据值。

我不完全确定如何结合这 2 ideas.I 感觉这要么是我没有得到的非常简单的东西,要么是我应该采用的完全不同的方法。

import java.util.scanner;

public class StudentNode extends Student
{
private Student data;
private StudentNode next;


class SinglyLinkedList
{

 private StudentNode first;

 //constructor
 public SinglyLinkedList()
 {
     first=null;
 }


 public addToList(Student newData)
 {
     StudentNode newNode= new StudentNode();
     newNode.data=newData;
     newNode.next=first; //refs to the element first is currently pointing to
     first=newNode;//first now refs to added element
 }           

 public courseMark(Student data)
 {
     double cm=courseMark(StudentNode.data);
     return "Student number : "+stuNum +"Course Mark: "+cm;

 }



public double classAverage(Student data)
{
//traverses linked list, not enirely sure about how to access the course mark
    double classAvg=0;
    double sum = 0;
    int i=0;
    StudentNode current = first;
    StudentNode previous = null;
    while (current != null) 
    {
        i++;  
        StudentNode current= Student.courseMark();
        sum += current.data;//not sure bout course mark access
        previous = current;
        current = current.next;
     }
   return classAvg=sum/i;
  }

这里是数据组件使用的 Student class。不确定是否需要回答。

public class Student
{
private String name;
private String stuNum;
private int firstTest;
private int secondTest;
private int thirdTest;

public Student(String n,String sN,int fT,int sT,int tT)
{
    name=n;
    stuName=sN;
    firstTest=fT;
    secondTest=sT;
    thirsTest=tT;
}

//setters
public void setName(String n)
{
    name=n;
}    

public void setStuNum(String sN)
{
    stuNum=sN;
}

public void setFirstTest(int fT)
{
    firstTest=fT;
}

public void setSecondTest(int sT)
{
    secondTest=sT;
}

public void setThirdTest(int tT)
{
    thirdTest=tT;
}

//getters

public String getName()
{
    return name;
}

public String getStuNum()
{
    return stuNum;
}

public int getFirstTest()
{
    return firstTest;
}

public int getSecondTest()
{
    return secondTest;
}

public int getThirdTest()
{
    return thirdTest;
}

//course mark computer

public double courseMark()
{
    double crseMark=(firstTest*0.25)+(secondTest*0.25)+(thirdTest*0.50);
    return crseMark;
}

}

必须从节点遍历到学生数据才能得到courseMark。

while (current != null) { ... double courseMark = current.data.courseMark(); ... }