ArrayList 및 LinkedList 클래스는 java.util의 List 인터페이스를 구현합니다. 패키지. 이 인터페이스는 remove()의 두 가지 변형을 제공했습니다. 아래와 같이 특정 요소를 제거하는 방법 -
-
E 제거(int 인덱스)
-
부울 제거(객체 o) -
이 방법 중 하나를 사용하여 List 또는 Java의 linkedList에서 원하는 요소를 삭제할 수 있습니다.
E 제거(int 인덱스) - 이 메소드는 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
부울 제거(객체 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() 메소드
이 두 가지 방법 외에도 remove()를 사용하여 LinkedList 또는 ArrayList 객체의 요소를 제거할 수도 있습니다. Iterator 클래스.
예시
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