【问题标题】:ArrayList remove element with index 0 and 1ArrayList 删除索引为 0 和 1 的元素
【发布时间】:2015-06-29 00:10:21
【问题描述】:

我想删除索引为 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

【问题讨论】:

    标签: java arraylist collections


    【解决方案1】:

    我相信这是因为您在 Collection 上调用 remove(int)。 Collection 没有声明 remove(int) 方法,但确实有 remove(Object),因此 java 正在将您的 int 自动装箱为 Integer。但由于该整数不在集合中,因此不会删除任何内容

    【讨论】:

      【解决方案2】:

      将您的 c 更改为 ArrayList 因为:

      Collection.remove(Object o)

      从该集合中移除指定元素的单个实例(如果存在)(可选操作)。更正式地说,删除元素 e 使得 (o==null ? e==null : o.equals(e))

      在上面如果是 c.remove("A"); 它将起作用。编写 c.remove(1); 是在寻找要删除的 Integer 对象。

      ArrayList.remove(int index)

      删除此列表中指定位置的元素。将任何后续元素向左移动(从它们的索引中减去 1)。

      所以你的程序应该如下:

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

      【讨论】:

        【解决方案3】:

        我认为你学到了重要的一课。

        问题是Collection不支持remove(int),只支持remove(Object)。所以编译器将你的 int 装箱为整数,该元素不在集合中,因此它不会删除它。

        如果您将集合声明为 ArrayList,则它可以工作。

        【讨论】:

          【解决方案4】:

          正如@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);
          }
          

          【讨论】:

            【解决方案5】:

            你真正需要的是

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

            如果你想继续使用Collection,而不是使用索引来引用它们。相同的原因是Collection 中的remove 方法需要Object 参数。因此,当您编写c.remove(0) 时,它会在列表中搜索元素0 并尝试将其删除。

            对于这种情况,请在使用 Collection 而不是 List 时考虑性能权衡。

            【讨论】:

              【解决方案6】:

              集合只是数据的集合,因此它们并不总是保持顺序。我建议用 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());
              
              
              
              }   
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-07-06
                • 1970-01-01
                • 1970-01-01
                • 2018-04-21
                • 2012-08-26
                • 1970-01-01
                • 2014-08-28
                • 2012-04-25
                相关资源
                最近更新 更多