Java – ArrayList deletes elements with indexes 0 and 1

I want to delete the elements in the ArrayList with indexes 0 and 1 But it doesn't work. I don't know how

The code is shown below

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());



    }   
}

Output is

A
B
C

A
B
C

But the output should be

A
B
C

C

Solution

I believe this is because you call remove (int) on the collection The collection does not declare the method remove (int), but it does have remove (object), so Java automatically boxed your int into an integer However, since the integer is not in the collection, nothing will be deleted

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>