我被分配了一个任务,以创建一个用 Java 编写的基于寄存器的小程序,以链表的形式。我首先创建了一个学生类,然后为该类创建了一个测试器文件。之后,在注册表文件中,我列出了我的方法和构造函数,并且正在编写一个测试器文件来测试我的所有方法。
但是,当我尝试从我的链表中删除特定元素时遇到问题,我希望能够删除一个学生,通过他们各自的学生 ID 引用他们,但我不确定如何执行此操作。
在尝试解决问题时,我遇到了该removeFirstOccurrence(Object o)
方法。这是正确的使用方法吗?
任何帮助将非常感激。
学生档案代码:
public class Student {
private String foreName;
private String surName;
private int studentID;
//declaring the variables needed for my student
public Student (String foreName, String surName, int studentID)
{
this.foreName = foreName;
this.surName = surName;
this.studentID = studentID;
}
//constructor to set out what a student needs
public String getForeName() {
return foreName;
}
public String getSurName() {
return surName;
}
public int getStudentID() {
return studentID;
}
public void setForeName(String foreName) {
this.foreName = foreName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
// getters and setters for my variables
public String toString ()
{
return getClass().getName() + "foreName = " + foreName + "surName = " + surName + "studentID = " + studentID;
}
//my toString method
}
注册表文件代码:
import java.util.*;
public class Registry {
LinkedList<String> studentList
= new LinkedList<String>();
//setting my type parameter
public Registry() {}
//empty constructor to hold arguements
public void addStudent(String aStudent)
{
this.studentList.addLast(aStudent);
}
public void deleteStudent(int studentID)
{
//????
}
@Override public String toString()
{
return "Registry";
}
public String format()
{
}
注册表测试器文件代码:
import java.util.*;
public class RegistryTester {
public static void main (String[] args)
{
LinkedList<String> studentList
= new LinkedList<String>();
System.out.println("Test 1");
System.out.println("Methods tested: addStudent, constructor");
System.out.println("********************");
studentList.add("Joe Perkins 123");
studentList.addLast("Shilpa Gupta 1234");
studentList.addLast("Seany Ray 12345");
// adding 3 students to my list
System.out.println(studentList);
}
}