如何在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 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告