logo

Usuń element z ArrayList w Javie

Lista tablic jest podobna do tablicy, której rozmiar można modyfikować. Klasa ArrayList jest dostępna w pliku Java.util pakiet i rozszerza Lista interfejs . Dodawanie i usuwanie elementu z ArrayList jest bardzo łatwe dzięki wykorzystaniu wbudowanych metod dodać() I usunąć() . Istnieje jednak więcej niż jeden sposób usunięcia elementu z listy ArrayList, a są to następujące:

zastąpienie metody Java
  1. Korzystanie z metody ArrayList.remove().
    1. Według indeksu.
    2. Według elementu
  2. Korzystanie z metody Iterator.remove().
  3. Korzystanie z metody ArrayList.removeIf().
Usuń element z ArrayList w Javie

Wszystkie te trzy sposoby są najlepsze same w sobie i można je zastosować w różnych scenariuszach. Rozumiemy wszystkie te trzy sposoby, jeden po drugim.

Metoda ArrayList.remove().

Używając usunąć() metoda Klasa ArrayList to najszybszy sposób usunięcia lub usunięcia elementu z listy ArrayList. Zapewnia również dwie przeciążone metody, tj. usuń(indeks int) I usuń (obiekt obiektu) . The usuń(indeks int) metoda akceptuje indeks obiektu, który ma zostać usunięty, oraz usuń (obiekt obiektu) metoda akceptuje obiekt do usunięcia.

Weźmy przykład, aby zrozumieć, jak usunąć() stosowana jest metoda.

UsuńMetodę.java

 import java.util.ArrayList; public class RemoveMethod { public static void main(String[] args) { // creating an ArrayList having default size 5 ArrayList arr = new ArrayList(5); // Adding elements to the ArrayList arr.add('Helen'); arr.add('Paul'); arr.add('Elanie'); arr.add('Marco'); System.out.println('The list of the size is: ' + arr.size()); // Showing all the elements in the ArrayList for (String name : arr) { System.out.println('Name is: ' + name); } // Removing element available at position 1 arr.remove(1); System.out.println('
After removing the element the size of the ArrayList is: ' + arr.size()); // Showing all the elements in the ArrayList for (String name : arr) { System.out.println('Name is: ' + name); } } } 

Wyjście:

Usuń element z ArrayList w Javie

Weźmy inny przykład, aby zrozumieć, w jaki sposób usunąć() Metoda służy do usunięcia określonego elementu z ArrayList.

UsuńElementMethod.java

gimp zapisuje jako JPEG
 import java.util.ArrayList; public class RemoveElementMethod { public static void main(String[] args) { // creating an ArrayList having default size 5 ArrayList arr = new ArrayList(5); // Adding elements to the ArrayList arr.add('Helen'); arr.add('Paul'); arr.add('Elanie'); arr.add('Marco'); System.out.println('The list of the size is: ' + arr.size()); // Showing all the elements in the ArrayList for (String name : arr) { System.out.println('Name is: ' + name); } // Removing the specified element from ArrayList arr.remove('Paul'); System.out.println('
After removing the element the size of the ArrayList is: ' + arr.size()); // Showing all the elements in the ArrayList for (String name : arr) { System.out.println('Name is: ' + name); } } } 

Wyjście:

kiedy wyszło win 7
Usuń element z ArrayList w Javie

Metoda Iterator.remove().

The Iterator.usuń() metoda to inny sposób usunięcia elementu z listy ArrayList. Nie jest to zbyt pomocne w przypadku iteracji po elementach. Kiedy używamy metody usuwania() podczas iteracji elementów, wyrzuca ona metodę Wyjątek ConcurrentModificationException . The Iterator klasa poprawnie usuwa elementy podczas iteracji ArrayList.

Weźmy przykład, aby zrozumieć, w jaki sposób używana jest metoda Iterator.remove().

IteratorRemoveMethod.java

 import java.util.ArrayList; import java.util.Iterator; public class iteratorRemoveMethod { public static void main(String[] args) { // creating an ArrayList having default size 10 ArrayList numbers = new ArrayList(10); // Adding elements to the ArrayList numbers.add(12); numbers.add(1); numbers.add(8); numbers.add(5); numbers.add(9); System.out.println('The list of the size is: ' + numbers.size()); // Showing all the elements in the ArrayList for (Integer number : numbers) { System.out.println('Number is: ' + number); } // Removing elements greater than 10 using remove() method Iterator itr = numbers.iterator(); while (itr.hasNext()) { int data = (Integer)itr.next(); if (data > 10) itr.remove(); } System.out.println('
After removing the element the size of the ArrayList is: ' + numbers.size()); // Showing all the elements in the ArrayList for (Integer number : numbers) { System.out.println('Number is: ' + number); } } } 

Wyjście:

Usuń element z ArrayList w Javie

Metoda ArrayList.removeIf().

Jeśli chcemy usunąć element z listy ArrayList, który spełnia filtr predykatów, metoda usuńIf() metoda jest w tym przypadku najbardziej odpowiednia. Przekazujemy filtr predykatów do tej metody jako argument.

Weźmy przykład, aby zrozumieć, jak usuńIf() stosowana jest metoda.

atoi c

UsuńIfMethod.java

 import java.util.ArrayList; public class RemoveIfMethod { public static void main(String[] args) { // creating an ArrayList having default size 10 ArrayList cities = new ArrayList(10); // Adding elements to the ArrayList cities.add('Berlin'); cities.add('Bilbao'); cities.add('Cape Town'); cities.add('Nazilli'); cities.add('Uribia'); cities.add('Gliwice'); System.out.println('The list of the size is: ' + cities.size()); // Showing all the elements in the ArrayList for (String city : cities) { System.out.println('City is: ' + city); } // Removing elements which are start with B using removeIf() method cities.removeIf(n -> (n.charAt(0) == 'B')); System.out.println('
After removing the element the size of the ArrayList is: ' + cities.size()); // Showing all the elements in the ArrayList for (String city : cities) { System.out.println('City is: ' + city); } } } 

Wyjście:

Usuń element z ArrayList w Javie

Wszystkie omówione powyżej metody są stosowane w różnych scenariuszach. Użycie metody ArrayList.remove() to najszybszy sposób usunięcia elementu z listy ArrayList.