Java 从链表中删除特定元素时遇到问题
我被指派创建一个用Java编写的基于寄存器的小程序,以链表的形式。我首先创建了一个student类,然后为该类创建了一个tester文件。接下来,在注册表文件中,我列出了我的方法和构造函数,我正在编写一个测试程序文件来测试我的所有方法 然而,当我试图从linkedlist中删除某个特定元素时遇到问题,我希望能够删除一个学生,通过他们各自的studentID引用他们,但不确定如何做到这一点 在试图解决这个问题时,我遇到了Java 从链表中删除特定元素时遇到问题,java,linked-list,Java,Linked List,我被指派创建一个用Java编写的基于寄存器的小程序,以链表的形式。我首先创建了一个student类,然后为该类创建了一个tester文件。接下来,在注册表文件中,我列出了我的方法和构造函数,我正在编写一个测试程序文件来测试我的所有方法 然而,当我试图从linkedlist中删除某个特定元素时遇到问题,我希望能够删除一个学生,通过他们各自的studentID引用他们,但不确定如何做到这一点 在试图解决这个问题时,我遇到了removeFirstOccurrence(objecto)方法。这是正确的方
removeFirstOccurrence(objecto)
方法。这是正确的方法吗
任何帮助都将不胜感激
学生档案代码:
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);
}
}
注册表文件代码:
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);
}
}
import java.util.*;
公共类注册表{
链接列表学生列表
=新链接列表();
//设置我的类型参数
公共注册表(){}
//用于保存参数的空构造函数
public void addStudent(字符串aStudent)
{
this.studentList.addLast(aStudent);
}
公共学生(国际学生ID)
{
//????
}
@重写公共字符串toString()
{
返回“注册表”;
}
公共字符串格式()
{
}
注册表测试程序文件代码:
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);
}
}
import java.util.*;
公共类注册测试器{
公共静态void main(字符串[]args)
{
链接列表学生列表
=新链接列表();
系统输出打印项次(“测试1”);
System.out.println(“测试方法:addStudent,constructor”);
System.out.println(“**********************”);
学生名单。添加(“Joe Perkins 123”);
学生名单。addLast(“Shilpa Gupta 1234”);
addLast(“Seany Ray 12345”);
//将3名学生添加到我的列表中
System.out.println(学生名单);
}
}
列表
定义为学生的列表
LinkedList<Student> studentList = new LinkedList<Student>();
public void addStudent(Student aStudent)
{
this.studentList.addLast(aStudent);
}
public void deleteStudent(Student astudent)
{
this.studentList.remove(aStudent)
}
如果您有一个
学生的集合(在您的例子中是一个链接列表),并在其上调用删除(studentToRemove)
,Java将使用其equals()
方法将每个对象与学生删除
进行比较
在您的情况下,您没有编写equals()
方法,因此使用了对象的方法。如果一个学生确实是由其studentId
唯一标识的(即,两个student
实例具有相同的studentId
始终是同一个学生),您应该重写equals()
metod并使用该字段检查是否相等
了解和了解
另一种选择是迭代列表,直到找到匹配项,然后从列表中删除该学生。首先,您应该为类型学生创建链接列表:
LinkedList<Student>
使用Map
,(HashMap
或LinkedHashMap
)而不是LinkedList
,键是studentId,值是studentObject。这将使添加/删除更容易
既然你有一个学生
班级,为什么不使用链接列表
?