如何从Java中的ArrayList或LinkedList中删除元素?
ArrayList和LinkedList类实现java.util包的List接口。该接口提供了remove()方法的两个变体,以删除特定的元素,如下所示-
Eremove(int索引)
布尔值remove(Objecto)-
使用这些方法之一,您可以从List或Java中的linkedList中删除所需的元素。
Eremove(intindex)-此方法接受表示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
booleanremove(Objecto)-此方法接受一个表示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