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

Question and solution: How to delete or add an object to/from array list while iterating it.
By iterating with a classic for ... 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 ... More on reddit.com
๐ŸŒ r/processing
7
9
December 28, 2022
performance - Deleting objects from an ArrayList in Java - Stack Overflow
So, depending on the objects in that list, it might not work as expected. Basically, if you could swap the ArrayList for a SortedSet without hurting your app, then you should be OK. 2009-08-21T07:55:43.22Z+00:00 ... This is what I see in javadoc for the remove() method of Iterator: "Removes from ... 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
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
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object ...
๐ŸŒ
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).
๐ŸŒ
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. Example: Java ยท // Java Program Illustrate List // remove(Element) Method ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-list-remove-methods-arraylist-remove
How To Use remove() Methods for Java List and ArrayList | DigitalOcean
September 9, 2025 - This method returns true if an element is removed from the list, otherwise false. If the object is null and list doesnโ€™t support null elements, NullPointerException is thrown. UnsupportedOperationException is thrown if the list implementation doesnโ€™t support this method. Letโ€™s look into some examples of remove() methods. ... List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("C"); list.add("B"); list.add("A"); System.out.println(list); String removedStr = list.remove(1); System.out.println(list); System.out.println(removedStr);
๐ŸŒ
Quora
quora.com โ€บ How-do-you-delete-an-object-from-an-ArrayList-in-Java
How to delete an object from an ArrayList in Java - Quora
Answer (1 of 5): An ArrayList is a dynamic array that is mutable (which can be changed by adding, editing, and/or removing elements). You use the .remove() function that takes in an int parameter for the index of the element that you wish to remove. * Note that indexes start at 0, so the index...
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
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

๐ŸŒ
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!

๐ŸŒ
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 - JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... Carey Brown wrote:First of all, your new list does not contain copies of all of the objects in the original list, it only contains copies of some of the ยท references, and references are quite small. Secondly, more often than not the new list is then assigned to the old list variable thus freeing up the old list for garbage collection. I tested the removeIf and stream method on ArrayList, this is the code that I use to test (extends from the loop code above): It is true that it's very fast, but I'm curious about the cost (memory, CPU) that it uses?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - Now, We will be discussing both ways via interpreting through a clean java program. ... There are 3 ways to remove an element from ArrayList as listed which later on will be revealed as follows:
๐ŸŒ
Processing Forum
forum.processing.org โ€บ beta โ€บ num_1275642093.html
Processing 1.0 - Processing Discourse - removing elements of an arrayList
June 4, 2010 - It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online. Index โ€บ Programming Questions & Help &rsaquo; Syntax Questions โ€บ removing elements of an arrayList
๐ŸŒ
Quora
quora.com โ€บ What-does-the-remove-of-the-arraylist-element-mean
What does the remove of the arraylist element mean? - Quora
Answer (1 of 2): Yep, when an item is removed from an ArrayList, all items after it will move forward to fill the space. If you remove element four (which is what list.remove(4); does), all the items after item number 5 (lists are zero indexed) will move forward. Now if you call list.get(4); yo...
๐ŸŒ
Brainly
brainly.com โ€บ computers and technology โ€บ college โ€บ how do you remove items from an arraylist object?
[FREE] How do you remove items from an ArrayList object? - brainly.com
September 2, 2023 - Objects in a Java ArrayList can be removed using either the remove(int index) method, if you know the index of the item to be removed, or the remove(Object o) method, if you have a specific object to remove and you don't know its index.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arraylist.asp
Java ArrayList
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want. To use an ArrayList, you must first import it from java.util: Create an ArrayList object called cars that will store strings:
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ a problem with java arraylist remove method
r/learnjava on Reddit: A problem with java arraylist remove method
November 6, 2018 -

Hello there...I'm creating a bank system using java,just for fun.Anyways,I've faced a problem and couldn't find a solution for it on .I want to delete all info about a customer(name,account number,balance),however, I keep getting this error.

Exception in thread "main" java.util.ConcurrentModificationException

at java.util.ArrayList$Itr.checkForComodification([ArrayList.java:909](https://ArrayList.java:909))

at [java.util.ArrayList$Itr.next](https://java.util.ArrayList$Itr.next)([ArrayList.java:859](https://ArrayList.java:859))

at banksystem2.BankSystem2.main([BankSystem2.java:112](https://BankSystem2.java:112))

C:\Users\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 36 seconds)

I can't post a pic for some reason.

Here's the code for my removal case(remove by account number)

case 3:

boolean Delete=true;

System.out.println("Enter the account number which you want to remove: ");

delete=in1.nextInt();

for(Customer c:list)

{

if(delete==c.getaccNum())

{ Delete=false;

System.out.println("HERE!! "+list.indexOf(c));

locate=list.indexOf(c);

list.remove(locate);

}

else

{

Delete=true;

}

}

if(Delete)

System.out.println("Invalid account number!!!");

break;

Can someone help me,pls?

๐ŸŒ
YouTube
youtube.com โ€บ watch
Search And Remove From ArrayLists (Java Tutorial)
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.