簡體   English   中英

ArrayList刪除索引為0和1的元素

[英]ArrayList remove element with index 0 and 1

我想刪除索引為0和1的ArrayList中的元素。但它不起作用,我不知道如何。

代碼如下所示

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



    }   
}

輸出是

A
B
C

A
B
C

但輸出應該是

A
B
C

C

我相信這是因為你在Collection上調用remove(int)。 Collection沒有聲明方法remove(int),但確實有remove(Object),因此java將你的int自動裝箱到一個Integer中。 但由於該整數不在Collection中,因此不會刪除任何內容

我想你已經找到了重要的一課。

問題是Collection不支持remove(int) ,只支持remove(Object) 所以編譯器將你的int裝入一個Integer,該元素不在集合中,因此它不會刪除它。

如果您將集合聲明為ArrayList,則可以正常工作。

將您的c更改為ArrayList,因為:

Collection.remove(對象o)

從此集合中移除指定元素的單個實例(如果存在)(可選操作)。 更正式地,刪除元素e (o == null?e == null:o.equals(e))

在上面,如果是c.remove(“A”); 它會工作。 c.remove(1); 正在尋找要刪除的Integer對象。

ArrayList.remove(int index)

刪除此列表中指定位置的元素。 將任何后續元素向左移位(從索引中減去一個)。

所以你的程序應該如下:

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

正如@ControlAltDel所提到的, Collection不支持remove(int)remove(Object) ,並且int正在自動裝箱到Integer並且Integer不在集合中; 所以什么都沒有刪除。

如果你想把c保存為集合,那么你可以使用Iterator.remove()刪除前兩個項目。 像這樣:

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

你真正需要的是

c.remove((String)"A");
c.remove((String)"B");

如果你想繼續使用Collection而不是使用索引來引用它們。 同樣的原因是Collection中的remove方法需要Object參數。 因此,當您編寫c.remove(0) ,它會在列表中搜索元素0並嘗試將其刪除。

對於這種情況,請在使用Collection而不是List時考慮性能權衡。

集合只是數據集合,因此它們並不總是保留順序。 我建議用ArrayList替換集合

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



}   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM