Java List remove()方法– ArrayList remove()

Java List remove() method is used to remove elements from the list. ArrayList is the most widely used implementation of the List interface, so the examples here will use ArrayList remove() methods.

Java List remove()方法用于从列表中删除元素。 ArrayListList接口使用最广泛的实现,因此此处的示例将使用ArrayList remove()方法。

Java列表remove()方法 (Java List remove() Methods)

There are two remove() methods to remove elements from the List.

有两种remove()方法可从列表中删除元素。

  1. E remove(int index): This method removes the element at the specified index and return it. The subsequent elements are shifted to the left by one place. This method throws IndexOutOfBoundsException is the specified index is out of range. If the List implementations doesn’t support this operation, UnsupportedOperationException is thrown.

    E remove(int index) :此方法删除指定索引处的元素并返回它。 随后的元素向左移动一位。 如果指定的索引超出范围,则此方法将引发IndexOutOfBoundsException 。 如果List实现不支持此操作,则抛出UnsupportedOperationException
  2. boolean remove(Object o): This method removes the first occurrence of the specified object. If the list doesn’t contain the given element, it remains unchanged. This method returns true if an element is removed from the list, otherwise false. If the object is null and list doesn’t support null elements, NullPointerException is thrown. UnsupportedOperationException is thrown if the list implementation doesn’t support this method.

    boolean remove(Object o) :此方法删除指定对象的第一次出现。 如果列表不包含给定元素,则它保持不变。 如果将元素从列表中删除,则此方法返回true ,否则返回false 。 如果对象为null,并且list不支持null元素,则抛出NullPointerException 。 如果列表实现不支持此方法,则抛出UnsupportedOperationException。

列出remove()方法示例 (List remove() method examples)

Let’s look into some examples of remove() methods.

让我们看一下remove()方法的一些示例。

1.删​​除给定索引的元素 (1. Remove element at given index)

List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("C");
list.add("B");
list.add("A");
System.out.println(list);

String removedStr = list.remove(1);
System.out.println(list);
System.out.println(removedStr);

Output:

输出:

[A, B, C, C, B, A]
[A, C, C, B, A]
B

2.具有remove(int index)方法的IndexOutOfBoundsException (2. IndexOutOfBoundsException with remove(int index) Method)

List<String> list = new ArrayList<>();
list.add("A");
String removedStr = list.remove(10);

Exception Output:

异常输出:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 10 out of bounds for length 1
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.remove(ArrayList.java:535)
	at com.journaldev.java.ArrayListRemove.main(ArrayListRemove.java:19)

3.不可修改列表remove()UnsupportedOperationException示例 (3. Unmodifiable List remove() UnsupportedOperationException Example)

List.of() method creates an unmodifiable list, so using remove() method will throw UnsupportedOperationException.

List.of()方法创建一个不可修改的列表,因此使用remove()方法将引发UnsupportedOperationException。

jshell> List<String> list = List.of("a", "b");
list ==> [a, b]

jshell> list.remove(1);
|  Exception java.lang.UnsupportedOperationException
|        at ImmutableCollections.uoe (ImmutableCollections.java:72)
|        at ImmutableCollections$AbstractImmutableList.remove (ImmutableCollections.java:108)
|        at (#64:1)

jshell> list.remove("a");
|  Exception java.lang.UnsupportedOperationException
|        at ImmutableCollections.uoe (ImmutableCollections.java:72)
|        at ImmutableCollections$AbstractImmutableCollection.remove (ImmutableCollections.java:79)
|        at (#65:1)

jshell>
List Remove Index Example

List remove(index) Example

列表删除(索引)示例

4.从列表中删除一个对象 (4. Removing an object from the list)

List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("C");
list.add("B");
list.add("A");
System.out.println(list);

boolean isRemoved = list.remove("C");
System.out.println(list);
System.out.println(isRemoved);

isRemoved = list.remove("X");
System.out.println(list);
System.out.println(isRemoved);

Output:

输出:

[A, B, C, C, B, A]
[A, B, C, B, A]
true
[A, B, C, B, A]
false

参考资料 (References)

翻译自: https://www.journaldev.com/31869/java-list-remove-methods-arraylist-remove

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值