0

I want to remove an instance of an ArrayList, but with the code I have now it lists the movies removed. I have an ArrayList that takes title, year, genre and balance. The goal is to remove from the list using only title and year.

I have tried a for-each loop, but that leads to ConcurrentModificationException error, and I am not sure how use use Iterators to make it work for this example.

It is supposed to remove the instance specified by title and year so that when it lists the moviesAvailable, the list would be updated. However when listing it shows all movies (probably because it is not actually removing the movie).

4

2 Answers 2

1

One way to remove the targeted movie is via the Collections.removeIf() method:

public void removeMovie() {
    System.out.println("\nRemoving a movie.");
    System.out.print("Enter the title of the movie: ");
    String title = In.nextLine();
    System.out.print("Enter the year: ");
    int year = In.nextInt();
    moviesAvailable.removeIf(movie -> movie.hasType(title, year));
    // other stuff...
}

Note

  • The removeIf method iterates through your collection and true to its name, removes an element (a movie in this case) which satisfy the condition.
  • If your entry was not deleted, please check to see if title and year are correct. For example, does title contain a trailing new line?
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, but when when it lists the movies whichever one I delete still shows up
Please check to see if your title and year are correct.
I checked this solution. It seems to work. I have used scanner to input movie and year, and the matching happens in a case sensitive manner.
0

you can do something like

 moviesAvailable.removeIf(m -> m.hasType("s",2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.