如何在Java中从ArrayList或LinkedList中删除元素?


ArrayList和LinkedList类实现了java.util包中的List接口。此接口提供了两种变体的remove()方法来删除特定元素,如下所示:

  • E remove(int index)

  • boolean remove(Object o) −

使用其中一种方法,您可以从Java中的List或LinkedList中删除所需元素。

E remove(int index) − 此方法接受一个整数,表示List对象中的特定位置,并删除给定位置的元素。如果删除操作成功,此方法将返回已删除的元素。

如果传递给此方法的索引值小于0或大于列表大小减1,则会引发IndexOutOfBoundsException异常。

示例

 在线演示

import java.util.ArrayList;
public class RemoveExample {
   public static void main(String[] args) {
      //Instantiating an ArrayList object
      ArrayList<String> arrayList = new ArrayList<String>();
      arrayList.add("JavaFX");
      arrayList.add("Java");
      arrayList.add("WebGL");
      arrayList.add("OpenCV");
      System.out.println("Contents of the Array List: "+arrayList);
      //Removing elements
      System.out.println("Elements removed: ");
      System.out.println(arrayList.remove(0));
      System.out.println(arrayList.remove(2));
      System.out.println(" ");
      //Instantiating an LinkedList object
      ArrayList<String> linkedList = new ArrayList<String>();
      linkedList.add("Krishna");
      linkedList.add("Satish");
      linkedList.add("Mohan");
      linkedList.add("Radha");
      System.out.println("Contents of the linked List: "+arrayList);
      //Removing elements
      System.out.println("Elements removed: ");
      System.out.println(linkedList.remove(0));
      System.out.println(linkedList.remove(2));
   }
}

输出

Contents of the Array List: [JavaFx, Java, WebGL, OpenCV]
Elements removed:
JavaFX
OpenCV

Contents of the linked List: [Java, WebGL]
Elements removed:
Krishna
Radha

boolean remove(Object o) − 此方法接受一个表示List中元素的对象,并删除给定元素的第一次出现。此方法返回一个布尔值,表示:

  • true,如果操作成功。

  • false,如果操作不成功。

示例

 在线演示

import java.util.ArrayList;
public class RemoveExample {
   public static void main(String[] args) {
      //Instantiating an ArrayList object
      ArrayList<String> arrayList = new ArrayList<String>();
      arrayList.add("JavaFX");
      arrayList.add("Java");
      arrayList.add("WebGL");
      arrayList.add("OpenCV");
      System.out.println("Contents of the Array List: "+arrayList);
      //Removing elements
      System.out.println("Elements removed: ");
      System.out.println(arrayList.remove("JavaFX"));
      System.out.println(arrayList.remove("WebGL"));
      System.out.println("Contents of the array List after removing elements: "+arrayList);
      System.out.println(" ");
      //Instantiating an LinkedList object
      ArrayList<String> linkedList = new ArrayList<String>();
      linkedList.add("Krishna");
      linkedList.add("Satish");
      linkedList.add("Mohan");
      linkedList.add("Radha");
      System.out.println("Contents of the linked List: "+linkedList);
      //Removing elements
      System.out.println("Elements removed: ");
      System.out.println(linkedList.remove("Satish"));
      System.out.println(linkedList.remove("Mohan"));
      System.out.println("Contents of the linked List after removing elements: "+linkedList);
   }
}

输出

Contents of the Array List: [JavaFX, Java, WebGL, OpenCV]
Elements removed:
true
true
Contents of the array List after removing elements: [Java, OpenCV]

Contents of the linked List: [Krishna, Satish, Mohan, Radha]
Elements removed:
true
true
Contents of the linked List after removing elements: [Krishna, Radha]

Iterator对象的remove()方法

除了这两种方法之外,您还可以使用Iterator类的remove()方法删除LinkedList或ArrayList对象的元素。

示例

 在线演示

import java.util.ArrayList;
import java.util.Iterator;
public class RemoveExample {
   public static void main(String[] args) {
      //Instantiating an ArrayList object
      ArrayList<String> arrayList = new ArrayList<String>();
      arrayList.add("JavaFX");
      arrayList.add("Java");
      arrayList.add("WebGL");
      arrayList.add("OpenCV");
      System.out.println("Contents of the Array List: "+arrayList);
      //Retrieving the Iterator object
      Iterator<String> it1 = arrayList.iterator();
      it1.next();
      it1.remove();
      System.out.println("Contents of the array List after removing elements: ");
      while(it1.hasNext()) {
         System.out.println(it1.next());
      }
      //Instantiating an LinkedList object
      ArrayList<String> linkedList = new ArrayList<String>();
      linkedList.add("Krishna");
      linkedList.add("Satish");
      linkedList.add("Mohan");
      linkedList.add("Radha");
      System.out.println("Contents of the linked List: "+linkedList);
      //Retrieving the Iterator object
      Iterator<String> it2 = linkedList.iterator();
      it2.next();
      it2.remove();
      System.out.println("Contents of the linked List after removing elements: ");
      while(it2.hasNext()) {
         System.out.println(it2.next());
      }
   }
}

输出

Contents of the Array List: [JavaFX, Java, WebGL, OpenCV]
Contents of the array List after removing elements:
Java
WebGL
OpenCV
Contents of the linked List: [Krishna, Satish, Mohan, Radha]
Contents of the linked List after removing elements:
Satish
Mohan
Radha

更新于:2020年2月17日

682 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.