That's fine, although return getBoxes().remove(index) would do the same thing in a single line.
That's fine, although return getBoxes().remove(index) would do the same thing in a single line.
Yes it is a valid way of doing it. Java works with values, not references.
java - What does remove() method in ArrayList datatype exactly returns - Stack Overflow
Does the remove() method for ArrayLists return a new instance of ArrayLists?
java - Remove method in ArrayList - Stack Overflow
List.remove()
It returns the element that was removed, as stated in the Javadoc:
E java.util.List.remove(int index)
Returns:
the element previously at the specified position
It only prints "a" and "c" because it only removed "a" and "c".
When removing elements from an ArrayList, the indices of the following elements are decremented. You have to account for that if you wish your loop to remove all elements. Otherwise you'll skip half of the elements.
for(int i=0;i<list.size();i++) {
Log.d("Removed+++++++++++","++++"+list.remove(i));
i--;
}
As Java API documentation states:
E remove(int index) - removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
You may want to change your loop to while-loop if you don't want to increment the index of element you want to remove in next loop as it seems unnecessary in your example (it caused problem in your for-loop, you didn't decrement this index inside the loop again).
ArrayList<String> list = new ArrayList<>(); list.add("a");
list.add("b");
list.add("c");
list.add("d");
while (list.size() > 0) {
Log.d("Removed+++++++++++","++++" + list.remove(0)); // queue.remove();
}
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?
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
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)