1

I am having some issues when deleting an element in one of my arrays.Students is the array I am trying to remove elements from. I am searching elements by each ID. The program works but then I receive an error. I know that I can use the indexOf method to find but that would let me start from scratch from the code that I have. Is there a way to delete an Element from an array with searching from an ID? And if so, is there a way to update the array?

For example

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]

This is my removeStudent method:

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

this is my main method::-------------------------------------------------------------------------------------------------------------------------------------- so I tried to use the remove.Student(studentId) and it ran fine but when I add students and then try to delete them the error shows up again

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)
Jose Beltran
  • 57
  • 1
  • 6
  • Does this answer your question? [Java ArrayList throws IndexOutOfBoundsException after removing items from the list](https://stackoverflow.com/questions/51654828/java-arraylist-throws-indexoutofboundsexception-after-removing-items-from-the-li) – Thiyagu Aug 31 '20 at 04:32
  • i dont understand what this is telling me – Jose Beltran Aug 31 '20 at 04:36

2 Answers2

3

Consider using a map to store each student by 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;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

The reason you are getting this error Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1602488517 out of bounds for length 2 is because you remove a student in the array by the studentId instead of the index:

change removeStudent to this:

    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");
    }
Qiu Zhou
  • 1,235
  • 8
  • 12
  • but i also forgot to declare a new arrays since we are updating the list. after the this.students.remove(i), we include students = new ArrayList<>(); return; – Jose Beltran Aug 31 '20 at 16:31