Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList remove element with index 0 and 1

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
like image 465
Tobias Mai Avatar asked Jun 29 '15 00:06

Tobias Mai


People also ask

How do I remove an element from an ArrayList at a specific index?

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).

How do I remove one element from an ArrayList?

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.

How do you remove an integer from an ArrayList?

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.


5 Answers

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

like image 125
ControlAltDel Avatar answered Oct 13 '22 00:10

ControlAltDel


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());
    }   
}
like image 45
Jyoti Prakash Avatar answered Oct 13 '22 01:10

Jyoti Prakash


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.

like image 21
arcy Avatar answered Oct 13 '22 02:10

arcy


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);
}
like image 1
MT0 Avatar answered Oct 13 '22 02:10

MT0


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.

like image 1
Bhoot Avatar answered Oct 13 '22 00:10

Bhoot