简体   繁体   English

无法从链接列表中删除特定元素

[英]Having trouble removing a specific element from a linked list

I have been set an assignment to create a small register based program written in Java, in the form of a linked list. 我已经设置了一个赋值,以链接列表的形式创建一个用Java编写的基于寄存器的小程序。 I started by creating a student class, and then a tester file for the class. 我首先创建了一个学生类,然后是该类的测试文件。 Following that, in the registry file I have set out my methods, and a constructor, and am in the process of writing a tester file to test all my methods. 接下来,在注册表文件中,我已经设置了我的方法和构造函数,并且正在编写测试文件以测试我的所有方法。

However I am having trouble when trying to remove a specific element from my linkedlist, I want to be able to remove a student, referencing them by their individual studentID, but am not sure how to do this. 但是我在尝试从链接列表中删除特定元素时遇到问题,我希望能够删除学生,通过他们的个人学生ID引用他们,但我不知道如何做到这一点。

Whilst trying to solve the problem I came across the removeFirstOccurrence(Object o) method. 在尝试解决问题的同时,我遇到了removeFirstOccurrence(Object o)方法。 Is this the right method to use? 这是正确的使用方法吗?

Any help would be much appreciated. 任何帮助将非常感激。

STUDENT FILE CODE: 学生文件代码:

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

}

REGISTRY FILE CODE: 注册档案代码:

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()
{
}

REGISTRY TESTER FILE CODE: 注册管理机构测试文件代码:

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);

}

}
  1. Define the List as a List of Student List定义为Student List

     LinkedList<Student> studentList = new LinkedList<Student>(); 
  2. Override the hashCode() and equals() method in Student class. 覆盖Student类中的hashCode()equals()方法。

     public boolean equals(Object obj) { if (obj instanceof Student) { return studentID == ((Student)obj).getStudentID(); } return false; } public int hashCode() { return studentID; } 
  3. Define methods: 定义方法:

     public void addStudent(Student aStudent) { this.studentList.addLast(aStudent); } public void deleteStudent(Student astudent) { this.studentList.remove(aStudent) } 

First you should make your LinkedList for type Student : 首先,您应该为类型Student创建LinkedList

LinkedList<Student>

Then to remove a student, you could : 然后要删除学生,您可以:

  • go through the list, find out the student object with same id, remove it 浏览列表,找出具有相同ID的学生对象,将其删除

  • override equals() and hashcode() method in your Student class, then 然后,在您的Student类中重写equals() and hashcode()方法

     public void deleteStudent(int studentID) { //getStudent object (stu) By the given ID studentList.remove(stu); } 
  • use Map , ( HashMap or LinkedHashMap ) instead of LinkedList , key is the studentId, value is the studentObject. 使用Map ,( HashMapLinkedHashMap )代替LinkedList ,key是studentId,value是studentObject。 This will make add/remove easier. 这将使添加/删除更容易。

If you have a Collection (in your case a LinkedList ) of Student s and call remove(studentToRemove) on it, Java will compare each object with studentToRemove by using its equals() method. 如果你有一个StudentCollection (在你的情况下是一个LinkedList )并且在其上调用remove(studentToRemove) ,Java将使用equals()方法将每个对象与studentToRemove进行比较。

In your case, you haven't written an equals() method, hence the one for Object is used. 在您的情况下,您还没有编写equals()方法,因此使用了Object的方法。 If indeed a student is uniquely identified by its studentId (that is, two Student instances with the same studentId are always the same student) you should override the equals() metod and check for equality using that field. 如果学生确实由studentId唯一标识(即,具有相同studentId两个Student实例始终是同一个学生),则应覆盖equals() metod并使用该字段检查是否相等。

Read about equals() and hashCode() . 阅读有关equals()hashCode()的信息

Another alternative would be to iterate the list until you find the match, and then remove the student from the list. 另一种方法是迭代列表,直到找到匹配项,然后从列表中删除该学生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM