The problem you have right now is you're just removing an element from the list's index based on user input while you should make sure list is of that size. Second thing, You can't remove an element from the list while browsing it via the forEach loop, you would get a ConcurrentModificationException , read concurrentModificationException. The way to do it would be using an iterator, check

Iterator<Passenger> itr = passengerList.iterator();
while (itr.hasNext()){
    Passenger p = itr.next();
    if (id == p.getId()) {
        itr.remove();
        break;
    }
}
Answer from Manish Karki on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
If a value is specified and multiple elements in the list have the same value then only the first one is deleted. If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example. ... T refers to the data type of items in the list. Remove an integer from the list by position and by value: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); list.add(1); list.remove(Integer.valueOf(1)); // Remove by object list.remove(1); // Remove by index System.out.println(list); } }
🌐
Reddit
reddit.com › r/processing › question and solution: how to delete or add an object to/from array list while iterating it.
r/processing on Reddit: Question and solution: How to delete or add an object to/from array list while iterating it.
December 28, 2022 - By iterating with a classic for loop from backwards, adding or removing an object to/from the end of the list wont effect the loop. So it should be: ... I was talking with u/AgardenerCoding about the problem. And he encouraged me to post this as it may be very useful for someone else searching for an answer to same problem. ... See also the retainAll and removeAll methods in the JavaDoc. Another option is to create a new ArrayList containing all the elements you want to remove, and after the loop completes call mylist.removeAll(toRemove) (or, if you're discarding most of the list, reverse that and use mylist.retainAll(toKeep).
Discussions

java - How to delete specific element from array list - Stack Overflow
Im trying but its not good. Here is all code: //creating passener object Passenger passenger1 = new Passenger(1, "Stefan", "Jankovic", "stefan@gmail.com", 300... More on stackoverflow.com
🌐 stackoverflow.com
How do I remove an object from an arraylist and delete it in java?
What sort of error does it give you? Also, don't use indexOf(foo) just to find the index of the thing you want to delete. Just do remove(foo). More on reddit.com
🌐 r/learnprogramming
13
5
March 11, 2021
Cannot remove odd elements from ArrayList in Java.
You're iterating forward in the list using an index. As you remove items the next index moves down so your next pass through the loop skips it. Note: You probably want to be using the index based remove List.remove(int) instead of the element based Collection.remove(T). Options: Use an iterator (which has a remove method) Iterate over the list backwards so that removed elements don't change the indexes of elements you haven't visited yet. Also, you really should be using generics, preventing someone passing a list (without seeing a warning) that doesn't contain integers (and removing the need for you to cast to integer). public void removeOdd(ArrayList list) { ... } ...and later you might consider accepting List instead of ArrayList so that other lists can be passed - though there's no way to ensure it's a modifiable list then. More on reddit.com
🌐 r/learnprogramming
8
1
September 15, 2022
Question and solution: How to delete or add an object to/from array list while iterating it.
By iterating with a classic for ... to/from the end of the list wont effect the loop. So it should be: ... I was talking with u/AgardenerCoding about the problem. And he encouraged me to post this as it may be very useful for someone else searching for an answer to same problem. ... See also the retainAll and removeAll methods in the JavaDoc. Another option is to create a new ArrayList containing all the elements you want to ... More on reddit.com
🌐 r/processing
7
9
December 28, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - It is a default method as soon as we do use any method over data structure it is basically operating over indexes only so whenever we do use remove() method we are basically removing elements from indices from an ArrayList. ArrayList class provides two overloaded remove() methods. remove(int index): Accepts the index of the object to be removed · remove(Object obj): Accepts the object to be removed · Let us figure out with the help of examples been provided below as follows: ... // Java program to Remove Elements from ArrayList // Using remove() method by indices // Importing required classe
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
🌐
Baeldung
baeldung.com › home › java › java list › removing an element from an arraylist
Removing an Element From an ArrayList | Baeldung
April 4, 2025 - Next, let’s see how to remove some elements from the sport List. ArrayList has two available methods to remove an element, passing the index of the element to be removed, or passing the element itself to be removed, if present.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 750418 › java › Remove-element-ArrayList-List-loop
Remove the element of ArrayList and List in for-loop indexes? (Java in General forum at Coderanch)
March 22, 2022 - Each remove() is moving a bunch of elements by one index position. Lots of unnecessary work. For whatever reason you need to use Java 7... you can still use the method I labeled as Iterator on LinkedList, or make new list. Those still work in JDK 7. I'm sorry if you really really want to use the same ArrayList you started with, but it's just a bad idea under Java 7.
🌐
Codecademy
codecademy.com › docs › java › arraylist › .remove()
Java | ArrayList | .remove() | Codecademy
March 21, 2022 - An element can be removed from an ArrayList instance by being passed to the .remove() method.
🌐
CodeChef
codechef.com › practice › arrays
Practice Arrays
Find the peak elements in an arrayPro · 950 · Count of MaximumPro · 1180 · CandiesPro · 1018 · Missing number in permutationPro · Easy · Single number in multiple numbersPro · Medium · Array - Pascals or Khayyams trianglePro · Medium · Check if the array is sortedPro · Medium · Remove Duplicates from Sorted ArrayPro ·
Rating: 4.5 ​ - ​ 3.15K votes
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
Automatic Resizing: When the internal array becomes full, the ArrayList automatically increases its capacity by creating a new larger array and copying the existing elements into it. Index-Based Access: Elements are stored in contiguous memory locations, allowing fast access using indexes (e.g., get(index)), typically in O(1) time complexity. Element Shifting on Insert/Delete: When an element is added or removed at a specific index, the elements after that position are shifted to maintain order.
Published   May 12, 2026
🌐
W3Schools
w3schools.com › JAVA › ref_arraylist_removeif.asp
Java ArrayList removeIf() Method
import java.util.ArrayList; public ... numbers.removeIf( n -> n % 2 == 0 ); System.out.println(numbers); } } ... The removeIf() method removes all elements from this list for which a condition is satisfied....
🌐
CodeGym
codegym.cc › java blog › java collections › how to remove an element from arraylist in java?
How to remove an element from ArrayList in Java | CodeGym
December 10, 2024 - As you can see, it did just what we wanted it to do :)" But the last one is "Fluffy" not "Messi". And if this is true that means that "if instruction" is unnecessary beacause Iterator doesent search for "Lionel Messi" but simply remove last element from ArrayList
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › map
Array.prototype.map() - JavaScript | MDN
3 weeks ago - When undefined or nothing is returned, the resulting array contains undefined. If you want to delete the element instead, chain a filter() method, or use the flatMap() method and return an empty array to signify deletion. ... const numbers = [1, 2, 3, 4]; const filteredNumbers = numbers.map((num, ...
🌐
Udemy
udemy.com › development
Selenium WebDriver with Java -Basics to Advanced+Frameworks
April 12, 2026 - Learn to use a for loop to iterate over arrays of strings and integers in Java, print each element, and manage array length with arr.length using zero-based indexing. Enhanced for loop declaration & using Conditional statements inside the loops11:20 · Explore enhanced for loop declaration in Java to iterate over arrays, print values, and apply if conditions inside loops to filter multiples of two, using break to stop early. ... Explore what an ArrayList is and why it replaces fixed-size arrays for dynamic data.
Rating: 4.6 ​ - ​ 141K votes
🌐
Kotlin
kotlinlang.org › docs › whatsnew23.html
What's new in Kotlin 2.3.0 | Kotlin Documentation
The new explicit syntax simplifies the common backing properties pattern where a property's internal type is different from its exposed API type. For example, you might use an ArrayList while exposing it as a read-only List or a MutableList.
🌐
Reddit
reddit.com › r/learnprogramming › how do i remove an object from an arraylist and delete it in java?
r/learnprogramming on Reddit: How do I remove an object from an arraylist and delete it in java?
March 11, 2021 -

So, basically I want to do a shopping list that you can put the price, quantity, name. I want to implement a button that deletes an object. I have tried doing "int s = objectList.indexOf(myobject); objectList.remove(s); myobject=null;", but it keeps giving me errors. Please help me, if you need any code from my project, I will give it. Btw, the object extends JPanel

🌐
GeeksforGeeks
geeksforgeeks.org › java › list-removeint-index-method-in-java-with-examples
List remove(int index) method in Java with Examples - GeeksforGeeks
November 29, 2024 - The remove(int index) method of List interface in Java is used to remove an element from the specified index from a List container and returns the element after removing it. It also shifts the elements after the removed element by 1 position ...
🌐
Coderanch
coderanch.com › t › 627559 › java › remove-item-Arraylist-looping
How to remove a item from Arraylist without looping it (Java in General forum at Coderanch)
January 25, 2014 - i have a Array-list which contain list of names.I need to remove one name.I can do this using for loop and find the index of element and delete it.But i need to delete it without looping.Is there any method to delete element by name? or can i use any other Data Structure in Java for this issue? ... You don't need to loop. There's already an API http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(java.lang.Object) ... Thanks Jaikiran. ... sam liyanage wrote:But i need to delete it without looping. I don't know where that requirement came from; let me point out that the ArrayList.remove(Object) very likely uses looping to achieve its goal.
🌐
Baeldung
baeldung.com › home › java › java collections › removing elements from java collections
Removing Elements from Java Collections | Baeldung
January 8, 2024 - Collection<String> names = new ArrayList<>(); names.add("John"); names.add("Ana"); names.add("Mary"); names.add("Anthony"); names.add("Mark"); Java’s Iterator enables us to both walk and remove every individual element within a Collection.