How to remove specific object from ArrayList in Java?
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;
}
Remove Item from ArrayList
In this specific case, you should remove the elements in descending order. First index 5
, then 3
, then 1
. This will remove the elements from the list without undesirable side effects.
for (int j = i.length-1; j >= 0; j--) {
list.remove(i[j]);
}
Removing elements from ArrayList
This however leaves "empty spaces" in my list.
No, it doesn't. It removes entries from the list completely. Other elements are moved appropriately. What it does do with the way you've written is skip the check for the next entry... because that will have "shuffled down" to be element i
, but you'll next look at element i + 1
.
One simple way to avoid this is to work backwards instead:
for (int i = myList.size() - 1; i >= 0; i--) {
if (myList.get(i).contains("foo")) {
myList.remove(i);
}
}
Or use an iterator as noted in other answers, of course. Both will work - the above code may be slightly more efficient if you're removing multiple entries, however, as there'll be less to shift by the time you get to the start. It's unlikely that that will be significant.
It's unfortunate that in order to use the iterator solution you have to use the iterator explicitly - you can't remove from a collection while using an enhanced for loop.
Android - Remove an item from arraylist by its ID
You can do like this
for(int i = 0 ; i < newsList.size() ; i++){
if("yourId".equalsIgnoreCase(newsList.get(i).id)){
newsList.remove(i);
}
}
Remove object by itself in ArrayList
You can use an Iterator, changing your attack
method to accept it as a parameter:
Iterator<Bullet> iterator = bulletList.iterator();
while (iterator.hasNext()) {
Bullet iBullet = iterator.next();
iBullet.move();
iBullet.attack(bulletList, iterator);
}
public void attack(ArrayList<Bullet> bulletList, Iterator<Bullet> iterator) {
iterator.remove();
}
Or you can change your attack
method to return a boolean indicating whether the bullet hit or not (instead of removing the bullet), and use the removeIf()
method introduced in Java 8:
for (Bullet iBullet : bulletList) {
iBullet.move();
}
bulletList.removeIf(b -> b.attack());
How to remove element from ArrayList?
You are facing ConcurrentModificationException
because you are doing two operations on the same list
at a time. i.e looping and removing same time.
Inorder to avoid this situation use Iterator,which guarantees you to remove the element from list safely .
A simple example looks like
Iterator<CartEntry> it = list.iterator();
while (it.hasNext()) {
if (it.next().getpId() == pId) {
it.remove();
break;
}
}
Java, how to remove an Integer item in an ArrayList
try this
list.removeAll(Arrays.asList(2));
it will remove all elements with value = 2
you can also use this
list.remove(Integer.valueOf(2));
but it will remove only first occurence of 2
list.remove(2)
does not work because it matches List.remove(int i)
which removes element with the specified index
Related Topics
Define a Fixed-Size List in Java
How to Enable Logging for SQL Statements When Using Jdbc
How to Convert a Java Object (Bean) to Key-Value Pairs (And Vice Versa)
Java.Lang.Outofmemoryerror: Java Heap Space in Maven
Java Swing: How to Implement a Login Screen Before Showing a Jframe
Java: Finding the Highest Value in an Array
How to Do a Soap Web Service Call from Java Class
Java Singleton and Synchronization
Overloading in Java and Multiple Dispatch
Why Do I Get "Non-Static Variable This Cannot Be Referenced from a Static Context"
Gson Custom Seralizer for One Variable (Of Many) in an Object Using Typeadapter
How to Fix "The Expression of Type List Needs Unchecked Conversion...'
In Java, How to Efficiently and Elegantly Stream a Tree Node's Descendants
Why Do == Comparisons with Integer.Valueof(String) Give Different Results for 127 and 128