I would like to remove the elements in an ArrayList with the index 0 and 1. But it does not work and I do not know way.
The code looks as follows
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test{
public static void main(String[] args){
Collection c = new ArrayList();
c.add("A");
c.add("B");
c.add("C");
for(Iterator i = c.iterator(); i.hasNext();)
System.out.println(i.next());
System.out.println("");
c.remove(1);
c.remove(0);
for(Iterator i = c.iterator(); i.hasNext();)
System.out.println(i.next());
}
}
The output is
A
B
C
A
B
C
But the output should be
A
B
C
C
The remove(int index) method present in java. util. ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices).
We can use the remove() method of ArrayList container in Java to remove the first element. ArrayList provides two overloaded remove() method: remove(int index) : Accept index of the object to be removed. We can pass the first element's index to the remove() method to delete the first element.
There are 3 ways to remove an element from ArrayList as listed which later on will be revealed as follows: Using remove() method by indexes(default) Using remove() method by values. Using remove() method over iterators.
I believe it is because you are calling remove(int) on a Collection
. Collection does not declare the method remove(int), but does have remove(Object), so java is autoboxing your int into an Integer. But since that integer isn't in the Collection, nothing is removed
Change your c to ArrayList because:
Collection.remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e))
In above if it was c.remove("A"); it will work. Writing c.remove(1); is looking for an Integer object to be removed.
ArrayList.remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
So your program should be as follow:
public class Test{
public static void main(String[] args){
ArrayList c = new ArrayList();
c.add("A");
c.add("B");
c.add("C");
for(Iterator i = c.iterator(); i.hasNext();)
System.out.println(i.next());
System.out.println("");
c.remove(1);
c.remove(0);
for(Iterator i = c.iterator(); i.hasNext();)
System.out.println(i.next());
}
}
I think you've hit on an important lesson.
The problem is that Collection does not support remove(int)
, only remove(Object)
. So the compiler is boxing your int as an Integer, that element is not in the collection, and therefore it isn't removing it.
If you declare the collection as an ArrayList instead, it works.
As mentioned by @ControlAltDel, Collection
does not support remove(int)
only remove(Object)
and the int
is being auto-boxed to Integer
and that Integer
is not in the collection; so nothing is removed.
If you want to keep c
as a collection then you can remove the first two items using Iterator.remove()
; like this:
public static void main( final String[] args ){
Collection<String> c = new ArrayList<>( Arrays.asList("A","B","C") );
for( String str : c )
System.out.println(str);
System.out.println();
Iterator<String> it = c.iterator();
it.next();
it.remove();
it.next();
it.remove();
for( String str : c )
System.out.println(str);
}
What you really need is
c.remove((String)"A");
c.remove((String)"B");
instead of referring to them using indices if you want to continue using Collection
. The reason for the same is that remove
method in Collection
expects Object
argument. Therefore, when you write c.remove(0)
, it searches the list for an element 0
and tries to remove it.
Please consider performance tradeoffs while using Collection
instead of List
for this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With