Java의 ArrayList에서 요소를 제거하는 방법은 무엇입니까?
ArrayList는 다음의 일부입니다. 수집 프레임워크 java.util 패키지에 있습니다. 이는 Java에서 동적 배열을 제공합니다. 하지만 표준 배열보다 느릴 수 있지만 배열에서 많은 조작이 필요한 프로그램에서는 도움이 될 수 있습니다. 이 클래스는 다음에서 찾을 수 있습니다. java.util 패키지. Java 버전의 도입 및 업그레이드를 통해 Java8 인식 람다 표현식 및 스트림 개념이 Java 버전 8에 도입된 것처럼 이전에는 사용할 수 없었던 경우 새로운 방법을 사용할 수 있으므로 Arraylist를 통해 작동할 수 있는 더 많은 방법이 있습니다. 작업을 수행합니다. 여기서는 ArrayList에서 요소를 제거하는 방법에 대해 설명합니다.
ArrayList에서 요소를 제거하는 동안 인덱스 위의 요소를 제거하거나 ArrayList에 있는 값을 통해 제거하도록 작업할 수 있습니다. 우리는 클린 자바 프로그램을 통한 해석을 통해 두 가지 방법에 대해 논의할 것입니다.
행동 양식:
있다 ArrayList에서 요소를 제거하는 3가지 방법 나열된 내용은 나중에 다음과 같이 공개됩니다.
- 인덱스별로 제거() 메소드 사용(기본값)
- 값으로 Remove() 메소드 사용
- 반복자에 대해 제거() 메서드 사용
메모: 요소를 반복할 때 ArrayList.remove()를 사용하지 않는 것이 좋습니다.
방법 1: 인덱스로 제거() 메소드 사용
이는 데이터 구조에 대한 메소드를 사용하자마자 기본 메소드이며 기본적으로 인덱스에 대해서만 작동하므로 제거() 메소드를 사용할 때마다 기본적으로 ArrayList의 인덱스에서 요소를 제거합니다.
ArrayList 클래스 두 개의 오버로드된 제거() 메서드를 제공합니다.
- Remove(int index): 제거할 객체의 인덱스를 허용합니다.
- Remove(Object obj): 제거할 객체를 수락합니다.
아래에 제공된 예를 통해 알아 보겠습니다.
예:
자바
// Java program to Remove Elements from ArrayList> // Using remove() method by indices> > // Importing required classes> import> java.util.ArrayList;> import> java.util.List;> > // Main class> public> class> GFG {> > // Main driver method> > public> static> void> main(String[] args)> > {> > // Creating an object of List interface with> > // reference to ArrayList class> > List al => new> ArrayList();> > > // Adding elements to our ArrayList> > // using add() method> > al.add(> 10> );> > al.add(> 20> );> > al.add(> 30> );> > al.add(> 1> );> > al.add(> 2> );> > > // Printing the current ArrayList> > System.out.println(al);> > > // This makes a call to remove(int) and> > // removes element 20> > al.remove(> 1> );> > > // Now element 30 is moved one position back> > // So element 30 is removed this time> > al.remove(> 1> );> > > // Printing the updated ArrayList> > System.out.println(al);> > }> }> |
산출
[10, 20, 30, 1, 2] [10, 1, 2]
이제 위의 인덱스를 통해 ArrayList의 요소를 제거하는 것을 보았으며 이제 전달된 매개변수가 인덱스로 간주되는지 살펴보겠습니다. 값으로 요소를 제거하는 방법.
방법 2: 값으로 Remove() 메소드 사용
예:
자바
// Java program to Remove Elements from ArrayList> // Using remove() method by values> > // Importing required classes> import> java.util.ArrayList;> import> java.util.List;> > // Main class> public> class> GFG {> > // Main driver method> > public> static> void> main(String[] args)> > {> > // Creating an object of List interface with> > // reference to ArrayList> > List al => new> ArrayList();> > > // Adding elements to ArrayList class> > // using add() method> > al.add(> 10> );> > al.add(> 20> );> > al.add(> 30> );> > al.add(> 1> );> > al.add(> 2> );> > > // Printing the current ArrayList> > System.out.println(al);> > > // This makes a call to remove(Object) and> > // removes element 1> > al.remove(Integer.valueOf(> 1> ));> > > // This makes a call to remove(Object) and> > // removes element 2> > al.remove(Integer.valueOf(> 2> ));> > > // Printing the modified ArrayList> > System.out.println(al);> > }> }> |
출력 :
[10, 20, 30,1 ,2] [10, 20, 30]
메모: 요소를 반복할 때 ArrayList.remove()를 사용하지 않는 것이 좋습니다.
또한 new Integer( int_value)는 Java 9부터 더 이상 사용되지 않으므로 Integer.valueOf(int_value)를 사용하여 기본 정수를 Integer Object로 변환하는 것이 더 좋습니다.
방법 3: Iterator.remove() 메소드 사용
이는 다음으로 이어질 수 있습니다. ConcurrentModificationException 요소를 반복할 때 다음을 사용하는 것이 좋습니다. 반복자.제거() 방법.
예:
자바
// Java program to demonstrate working of> // Iterator.remove() on an integer ArrayList> import> java.util.ArrayList;> import> java.util.Iterator;> import> java.util.List;> > public> class> GFG {> > > // Main driver method> > public> static> void> main(String[] args)> > {> > // Creating an ArrayList> > List al => new> ArrayList();> > > // Adding elements to our ArrayList> > // using add() method> > al.add(> 10> );> > al.add(> 20> );> > al.add(> 30> );> > al.add(> 1> );> > al.add(> 2> );> > > // Printing the current ArrayList> > System.out.println(al);> > > // Creating iterator object> > Iterator itr = al.iterator();> > > // Holds true till there is single element> > // remaining in the object> > while> (itr.hasNext()) {> > > // Remove elements smaller than 10 using> > // Iterator.remove()> > int> x = (Integer)itr.next();> > if> (x <> 10> )> > itr.remove();> > }> > > // Printing the updated ArrayList> > System.out.print(al);> > }> }> |
산출
[10, 20, 30, 1, 2] [10, 20, 30]