Java 如何从ArrayList中删除一个元素
ArrayList是 集合框架 的一部分,存在于java.util包中。它为我们提供了Java中的动态数组。尽管它可能比标准数组慢,但在需要对数组进行大量操作的程序中是很有帮助的。这个类在 java.util 包中可以找到。随着java版本的引入和升级,更新的方法正在出现,如果我们从Java8的感知中看到lambda表达式和流的概念之前是没有的,因为它是在java版本8中引入的,所以我们有更多的方法来操作Arraylist来执行操作。这里我们将讨论从ArrayList中删除一个元素的方法。
当从ArrayList中删除元素时,我们可以通过索引或通过ArrayList中的值来操作删除元素。我们将通过一个干净的java程序来讨论这两种方式。
方法
有 3种方法可以从ArrayList中删除一个元素 ,这些 方法 将在后面揭示。
- 通过索引使用remove()方法(默认)
- 通过值使用remove()方法
- 在迭代器上使用remove()方法
注意: 当对元素进行迭代时,不建议使用ArrayList.remove()方法。
方法1: 通过索引使用remove()方法
这是一个默认的方法,只要我们在数据结构上使用任何方法,它基本上只在索引上操作,所以每当我们使用remove()方法时,基本上都是从ArrayList的索引中删除元素。
ArrayList类提供了两个重载的remove()方法。
- 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<Integer> 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<Integer> 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()。
另外,新的Integer( int_value)在Java 9中已经被废弃,所以最好使用Integer.valueOf(int_value)将原始整数转换为Integer对象。
方法3: 使用Iterator.remove()方法
这可能会导致ConcurrentModificationException 当对元素进行迭代时,建议使用Iterator.remove()方法。
例子
// 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<Integer> 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]