ArrayList removes objects based on the equals(Object obj) method. So you should implement properly this method. Something like:

public boolean equals(Object obj) {
    if (obj == null) return false;
    if (obj == this) return true;
    if (!(obj instanceof ArrayTest)) return false;
    ArrayTest o = (ArrayTest) obj;
    return o.i == this.i;
}

Or

public boolean equals(Object obj) {
    if (obj instanceof ArrayTest) {
        ArrayTest o = (ArrayTest) obj;
        return o.i == this.i;
    }
    return false;
}
Answer from Valchev on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_remove.asp
Java ArrayList remove() Method
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); } }
Discussions

How to remove specific object from ArrayList in Java? - Stack Overflow
Java 8 has a removeIf method in the collection interface. For the ArrayList, it has an advanced implementation (order of n). ... Save this answer. ... Show activity on this post. In general an object can be removed in two ways from an ArrayList (or generally any List), by index (remove(int)) ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Remove object from ArrayList with some Object property - Stack Overflow
I am maintaining one ArrayList of objects. And my object structure is Id, name, some other details. I need to remove one the object with some id value say(10) and I don't want to iterate over the l... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to remove object from ArrayList depending on attribute?
You're hitting this warning spelled out in the javadocs for ArrayList: The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. What this means is you can't iterate through an ArrayList using a for loop and remove elements in that for loop. You can use an iterator to solve this. Call ArrayList.iterator() and use Iterator.remove() to do the removal. Iterator iter = phonebook.iterator(); while (iter.hasNext()) { Entry entry = iter.next(); if (entry.getFirstName().equalsIgnoreCase(string)) { iter.remove(); } } It's a bit clunky but that's how you modify a collection during iteration. More on reddit.com
๐ŸŒ r/javahelp
10
12
April 30, 2019
java - Removing items from a list - Stack Overflow
Getting a ConcurrentModificati... element from a java.util.List during list iteration? [duplicate] (11 answers) Closed 12 years ago. While looping through a list, I would like to remove an item of a list depending on a condition. See the code below. This gives me a ConcurrentModification exception. Copyfor (Object a : list) ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ list-removeobject-obj-method-in-java-with-examples
List remove(Object obj) method in Java with Examples - GeeksforGeeks
November 29, 2024 - The remove(Object obj) method of List interface in Java is used to remove the first occurrence of the specified element obj from this List if it is present in the List.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java โ€บ util โ€บ arraylist_remove_object.htm
Java.util.ArrayList.remove(Object) Method
The java.util.ArrayList.remove(Object) method removes the first occurrence of the specified element from this list, if it is present.If the list does not contain the element, it is unchanged.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ removing elements from java collections
Removing Elements from Java Collections | Baeldung
January 8, 2024 - In Java 8, ArrayList overrides the default implementation โ€“ which relies on Iterator โ€“ and implements a different strategy: first, it iterates over the elements and marks the ones that match our Predicate; afterward, it iterates a second time to remove (and shift) the elements that were marked in the first iteration. One of the new major features in Java 8 was the addition of Stream (and Collectors). There are many ways to create a Stream from a source.
Find elsewhere
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ java-arraylist-removeobject-method-example
Java ArrayList remove(Object obj) Method example
June 1, 2024 - We can remove a specific string from the list using remove() method. import java.util.ArrayList; public class RemoveExample { public static void main(String args[]) { //String ArrayList ArrayList<String> al = new ArrayList<>(); al.add("AA"); al.add("BB"); al.add("CC"); al.add("DD"); al.add("EE"); ...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ arraylist removeif(): remove elements matching a condition
ArrayList removeIf(): Remove Elements Matching a Condition
August 7, 2023 - Java ArrayList.removeIf() removes all elements that satisfy a condition by iterating through the elements and matching against the specified Predicate.
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 03 โ€บ 2-ways-to-remove-elementsobjects-from-ArrayList-java.html
2 Ways to Remove Elements/Objects From ArrayList in Java [Example] | Java67
... There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java โ€บ util โ€บ arraylist_remove.htm
Java ArrayList remove() Method
The Java ArrayList remove(Object) method removes the first occurrence of the specified element from this list, if it is present.If the list does not contain the element, it is unchanged.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ remove() in java
remove() in Java - Scaler Topics
April 7, 2024 - The remove method is often used in the Java Collection framework. The remove method removes the specified element from any collection of objects. However, the ways to remove an object might differ in one case or the other. The remove() method in ArrayList allows you to remove an element in two different ways. To begin with, you are supposed to know the object itself to get it removed from the list...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-remove-an-element-from-a-java-list
How to remove an element from a Java List?
May 26, 2025 - The remove(object) method of the List interface is used to remove the first occurrence of the specified element (i.e., object) from the list if it is present.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist remove(): remove a single element from list
Java ArrayList remove(): Remove a Single Element from List with Examples - HowToDoInJava
August 7, 2023 - If the list does not contain the element, the list remains unchanged. The remove() method is overloaded and comes in two forms: boolean remove(Object o) โ€“ removes the first occurrence of the specified element by value from the list.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2014 โ€บ 01 โ€บ ow-to-remove-objects-from-collection-arraylist-java-iterator-traversing.html
How to Remove Objects from Collection or List in Java? Iterator remove() method Example
May 10, 2023 - How do you remove objects from Java collections like ArrayList, while iterating is one of the frequent questions my reader asked me in my post about Top 25 Java Collection Interview Questions. Well, this question may seem quite easy, because of every java.util.Collection implementation like List or Set has remove() method to delete a particular object, which can be used to remove elements from any Collection like ArrayList, LinkedList, or Vector.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ removing an element from an arraylist
Removing an Element From an ArrayList | Baeldung
April 4, 2025 - Using remove passing an index as parameter, we can remove the element at the specified position in the List and shift any subsequent elements to the left, subtracting one from their indices.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - Also new Integer( int_value) has been deprecated since Java 9, so it is better idea to use Integer.valueOf(int_value) to convert a primitive integer to Integer Object. ... This may lead to ConcurrentModificationException When iterating over elements, it is recommended to use Iterator.remove() method. ... // Java program to demonstrate working of // Iterator.remove() on an integer ArrayList import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class GFG { // Main driver method public static void main(String[] args) { // Creating an ArrayList List<Integer> al = new
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how to remove object from arraylist depending on attribute?
r/javahelp on Reddit: How to remove object from ArrayList depending on attribute?
April 30, 2019 -

I am creating a phone book program and need a method to remove an entry from the directory depending on what name or phone number the user enters.

So far I have the following code:

public void deleteEntry(String string) {
        for(Entry e : phonebook){
            if (e.getFirstName().equalsIgnoreCase(string)){
                phonebook.remove(e);
                System.out.println("Entry " + e.toString() + " removed");
            }
            else {
                System.out.println("Entry not deleted");
            }
        }
    }

Which I thought should work, however it gives the following errors. The program outputs the deletion message as well which is weird so I am not sure what is causing this error.

The code the method is being called in is shown here:

                case 2:
                    printDirectory();
                    System.out.println("Enter name or number: ");
                    String nameOrNum = input.nextLine();
                    deleteEntry(nameOrNum);
                    break;

I have an Entry class which contains information for a single Entry object. Is there a way I could override the equals() method to compare first names and implement the delete method this way? Or can my solution be resolved easily?

Thanks in advance!