That's fine, although return getBoxes().remove(index) would do the same thing in a single line.

Answer from Louis Wasserman on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
import java.util.ArrayList; public ... an item from the list, either by position or by value. If a position is specified then this method returns the removed item....
Discussions

java - What does remove() method in ArrayList datatype exactly returns - Stack Overflow
I was trying to pass the returned value as a param to some function as a string so that it does both the job of returning as well as removing the object. When I print it in for loop it displays only two elements when it should print 4 . Why is it happening so ? ArrayList list = new ... More on stackoverflow.com
🌐 stackoverflow.com
Does the remove() method for ArrayLists return a new instance of ArrayLists?
the ArrayList instance will remain the same. for removing elements the ArrayList will not create a new array to back its data. Instead it will just shift the elements after the one that was removed to the left and readjust its size field value. The size of the underlaying array is not changed, only the window of it that the ArrayList considers valid data changes. for people interested in the actual code (taken from Java 11): public E remove(int index) { Objects.checkIndex(index, size); // elementdata is the array that contains the lists data final Object[] es = elementData; u/SuppressWarnings("unchecked") E oldValue = (E) es[index]; fastRemove(es, index); return oldValue; } private void fastRemove(Object[] es, int i) { modCount++; final int newSize; if ((newSize = size - 1) > i) System.arraycopy(es, i + 1, es, i, newSize - i); es[size = newSize] = null; } More on reddit.com
🌐 r/learnjava
6
37
July 5, 2020
java - Remove method in ArrayList - Stack Overflow
data = [0, 1, 2, 3, 4, 5, 6, 7, ... the value is the same as the index [0, 1, 2, 4, 5, 6, 7, 8, 9] It is a mix of add and remove methods badly implemented on the index. To remove an element from an array, you only need to shift every item after the index on the left. I also change the return value to match ... More on stackoverflow.com
🌐 stackoverflow.com
List.remove()
Another option beyond those already given is .removeIf(o -> o == objectToRemove) More on reddit.com
🌐 r/java
47
50
October 23, 2025
🌐
Programiz
programiz.com › java-programming › library › arraylist › remove
Java ArrayList remove()
In the above example, we have created an arraylist named languages. Notice the expression, ... Here, the remove() returns and removes the element present at position 2 (i.e.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-remove-methods-arraylist-remove
How To Use remove() Methods for Java List and ArrayList | DigitalOcean
Learn how to use the remove() method in Java’s List and ArrayList interfaces with examples for removing by index or object.
🌐
Javatpoint
javatpoint.com › java-arraylist-remove-method
Java ArrayList remove() method with Examples - Javatpoint
The remove() method of Java ArrayList class removes the first matching object in the ArrayList · Return "true": If this list contained the specified object
🌐
Reddit
reddit.com › r/learnjava › does the remove() method for arraylists return a new instance of arraylists?
r/learnjava on Reddit: Does the remove() method for ArrayLists return a new instance of ArrayLists?
July 5, 2020 -

I understand that when you delete or remove the element from an array list, we may need to shift over certain elements to get proper indexing, but what happens to the empty index space with empty memory? Does the remove method end up just returning a new instance of array list?

Find elsewhere
🌐
TutorialKart
tutorialkart.com › java › java-arraylist-remove
Java ArrayList remove()
August 20, 2023 - ArrayList remove() removes the element at the specified position in this ArrayList, and returns the removed object.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraylist_remove.htm
Java ArrayList remove() Method
Following is the declaration for java.util.ArrayList.remove() method ... This method returns the element that was removed from the list .
Top answer
1 of 2
1

Your method doesn't remove at all.

Using : (not java code)

data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
data.remove(3, 99) // 3 is the index, not the value.

First, the loop shift every on the right (start one cell before the index).

[0, 1, 2, 2, 3, 4, 5, 6, 7, 8]

Losing the last value (hidden by the sizevariable), it is actually at data[size]

Then it set Object o at the index.

[0, 1, 2, 99, 3, 4, 5, 6, 7, 8]

And decrements the size, losing another value.

[0, 1, 2, 99, 3, 4, 5, 6, 7]

Where the result should be

data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
data.remove(3); //the index, not the value but in this example, the value is the same as the index
[0, 1, 2, 4, 5, 6, 7, 8, 9] 

It is a mix of add and remove methods badly implemented on the index.


To remove an element from an array, you only need to shift every item after the index on the left. I also change the return value to match List.remove by return the value removed. (You could/should implements List<T> to get a correct Collection)

private T remove(int index){
    //keep the value to return at the end        
    T t = data[index];

    //Shift from index to the end
    for(int i = index; i < size - 1; ++i){
        data[index] = data[index + 1];
    }

    //remove the reference for an eventual GC visibility (prevent memory leaks)
    data[size - 1] = null;
    size--;

    return t;
}

Set null on the last cell to be sure to release the reference for the GC. And of course, decrement the size.

This is not safe to use, this requires some bounds verifications ! This could throw a ArrayIndexOutOfBoundsException for the moment

2 of 2
-3

Your Code does not remove any items from your list. If you want to remove something use the methods that are already provided: list.remove(index) or list.remove(Object)

🌐
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 - Shifts any subsequent elements to the left. Returns the removed element from the list. Throws IndexOutOfBoundsException if the argument index is invalid. Java program to remove an object from an ArrayList using remove() method.
🌐
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.
🌐
W3Resource
w3resource.com › java-tutorial › arraylist › arraylist_remove.php
Java arraylist remove Method - w3resource
August 19, 2022 - The remove() method is used to remove an element at a specified index from ArrayList. Shifts any subsequent elements to the left (subtracts one from their indices). ... Return Value: The element at the position next to the removed element.
🌐
Scaler
scaler.com › home › topics › remove() in java
remove() in Java - Scaler Topics
April 7, 2024 - The return value depends on the parameter of the remove method. Since character A is present inside the list, the remove method will remove the character and return true. But what if we pass a character not present in the ArrayList?
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-remove-method-example
Java ArrayList remove(int index) Method example
September 11, 2022 - Method remove(int index) is used for removing an element of the specified index from a list. It removes an element and returns the same. It throws IndexOutOfBoundsException if the specified index is less than zero or greater than the size of the list (index size of ArrayList).
🌐
Tpoint Tech
tpointtech.com › java-arraylist-remove-method
Java ArrayList remove() method with Examples - Tpoint Tech
March 21, 2025 - Java ArrayList () Method The () ... &quot;c&quot;: collection that contained elements to be removed from this list. Return: True if the original list changed as a result of this......
🌐
Codecademy
codecademy.com › docs › java › arraylist › .remove()
Java | ArrayList | .remove() | Codecademy
March 21, 2022 - The .remove() method is used for removing specified elements from instances of the ArrayList class.
🌐
W3Resource
w3resource.com › java-tutorial › arraylist › arraylist_remove-object-o.php
Java remove first character from arraylist Method - w3resource
August 19, 2022 - remove(Object o) Parameters: Return Value: true if this list contained the specified element. Return Value Type: boolean · Pictorial presentation of ArrayList.remove(Object o) Method · Example: ArrayList.remove(Object o) Method · The following ...
🌐
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 - The iterator is smarter than it may appear: remove() removes the last element returned by the iterator. As you can see, it did just what we wanted it to do :) In principle, this is everything you need to know about removing elements from an ...