【问题标题】:Deleting an element from an array using an ID使用 ID 从数组中删除元素
【发布时间】:2020-08-31 14:13:45
【问题描述】:

我在删除其中一个数组中的元素时遇到了一些问题。Students 是我要从中删除元素的数组。我正在按每个 ID 搜索元素。该程序有效,但随后我收到错误消息。我知道我可以使用 indexOf 方法来查找,但这会让我从我拥有的代码从头开始。有没有办法通过从 ID 搜索从数组中删除元素?如果是这样,有没有办法更新数组?

例如

private final ArrayList<Student> students;
students.add(1)//consider nums as ID
students.add(2)
students.add(3)

students = [1,2,3]

students.remove[1]

students = [2,3]

这是我的 removeStudent 方法:

public void removeStudent(int studentId) {

        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() == studentId) {

                System.out.println("Deleted a profile containing information for " + studentId + ":");
//                this.students.remove(studentId);//this is the first one I used that gave me an error but ran
                this.students.removeIf(student -> student.getId().equals(studentId));//this one I got from another response but does not work.
            }
        }
        System.out.println("Could not find profile with that ID");

这是我的 ma​​in 方法::-------------------------------- -------------------------------------------------- -------------------------------------------------- - 所以我尝试使用 remove.Student(studentId) 并且运行良好,但是当我添加学生然后尝试删除他们时,错误再次出现

public static void deleteStudentID() {
        System.out.println("Please enter your student ID to search for your profile");
        int searchId = scanner.nextInt();//asks user to insert an ID
        institution.removeStudent(searchId);//finds the class and searches the Id in array

        showOptions();
    }
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1602488517 out of bounds for length 2
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:373)
    at java.base/java.util.ArrayList.remove(ArrayList.java:503)
    at Institution.removeStudent(Institution.java:133)
    at Main.deleteStudentID(Main.java:237)
    at Main.deleteID(Main.java:220)
    at Main.main(Main.java:35)

【问题讨论】:

标签: java arrays intellij-idea


【解决方案1】:

考虑使用 map 按 ID 存储每个学生:

Map<Integer, Student> students = new HashMap<>();

// returns true if student found and deleted, false otherwise
public boolean removeStudent(int studentId) {
    students.remove(studentId) == null ? false : true;
}

【讨论】:

  • 问题在于我添加了两种以上的类型
  • 私人决赛ArrayList学生;
  • 我的回答建议使用地图,而不是直接修复/回答您当前的问题,而是作为通过 ID 搜索学生的更好更快的方式。在 hashmap 中查找是 O(1),而在列表中查找是 O(n)
  • 哦,我明白了,因为哈希图在常量下运行,而列表在线性下
  • @JoseBeltran 添加超过 2 种类型是什么意思?
【解决方案2】:

您收到此错误Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1602488517 out of bounds for length 2 的原因是因为您通过studentId 而不是index 在数组中删除了一个学生:

removeStudent 更改为:

    public void removeStudent(int studentId) {

      for (int i = 0; i < students.size(); i++) {
          if (students.get(i).getId() == studentId) {

              System.out.println("Deleted a profile containing information for 
                   " + studentId + ":");
              this.students.remove(i);
          }
      }
      System.out.println("Could not find profile with that ID");
    }

【讨论】:

  • 但我也忘了声明一个新数组,因为我们正在更新列表。在this.students.remove(i)之后,我们包括students = new ArrayList();返回;
猜你喜欢
  • 2021-12-25
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多