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()方法用于从列表中删除元素。 ArrayList是List接口使用最广泛的实现,因此此处的示例将使用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 1at 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

列表删除(索引)示例

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)

  • List remove() API Doc列出remove()API文档

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

Java List remove()方法– ArrayList remove()相关推荐

  1. 【Java集合系列】---ArrayList

    开篇前言--ArrayList中的基本方法 前面的博文中,小编主要简单介绍java集合的总体架构,在接下来的博文中,小编将详细介绍里面的各个类,通过demo.对比,来对java集合类进行更加深入的理解 ...

  2. ArrayList的remove方法(重写equals方法) 与LinkedList的常用操作

    package C12_18;import java.util.ArrayList;public class joy {public static void main(String[] args) { ...

  3. arraylist remove() java_执行ArrayList的remove(object)方法抛异常?

    简介 或许有很多小伙伴都尝试过如下的代码: 然后会发现抛出java.util.ConcurrentModificationException异常,这是一个并发异常.那么这个到底是什么情况?首先需要介绍 ...

  4. java的remove iterator_Java集合 iterator.remove()方法详解

    直接上代码: public classtest {public static voidmain(String[] args) { List list = new ArrayList<>() ...

  5. ArrayList中remove()方法删除元素之后下标重定位的问题

    需求: 有一个ArrayList数组,要求删除长度大于5的字符串,如:arr = {"ab1","123ad","bca","da ...

  6. Java List 的 remove 方法

    Java List 的 remove 方法有两个重载,一个接收 int 型参数(通过下标删除),一个接收 Object 型参数(通过元素删除).对于 List<Integer>,如果传入一 ...

  7. Java中ArrayList remove会遇到的坑

    前言 平时最常用的莫过于ArrayList和HashMap了,面试的时候也是问答的常客.先不去管容量.负载因子什么的,就是简单的使用也会遇到坑. Remove 元素 经常遇到的一个场景是:遍历list ...

  8. java remove map_Java HashMap remove()方法

    Java HashMap remove()方法 java.util.HashMap.remove(Object key, Object value) 方法从Map中删除具有关联的指定键的指定值. 1 ...

  9. ArrayList的remove方法

    从一个ArrayList中去除某个元素时会用到remove方法,这个方法有两个版本 public E remove(int index) public boolean remove(Object o) ...

最新文章

  1. Python 安装selenium
  2. 【解决方案】UserWarning: Possibly corrupt EXIF data.导致读取图片失败
  3. 使用android studio查看内存,Android Studio Profiler使用心得 检测内存泄露问题
  4. L、TEXT()、_TEXT()和_T()的区别
  5. jsp springmvc 视图解析器_SpringMVC学习笔记
  6. c语言无法打开源文件xx.h,VS2015 + Qt5.9.2开发中无法打开源文件“ui_*.h” 和 error MSB6006: “cmd.exe”已退出,代码为 3之解决办法。...
  7. 【人体姿态估计1】Convolutional Pose Machines_2016
  8. Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream 数学 + 暴力
  9. gerrit与crowdid, openid集成,设置openIdSsoUrl 直接登录
  10. 设计素材|美丽的几何和多边形背景纹理
  11. php7cms框架,GitHub - itsky71/itskycms: 基于ThinkPHP框架的一个CMS系统
  12. android调用摄像头拍照
  13. Jenkins+Git+Maven+Nexus+Tomcat
  14. 深海迷航创造模式火箭怎么飞_《我的世界》怎么用火箭使鞘翅飞起来?
  15. Unity 键盘控制摄像机镜头旋转,并限制旋转角度
  16. qemu: usb存储设备仿真
  17. Luogu 3537 [POI2012]SZA-Cloakroom
  18. 【Android开发经验】Android相关问题的好文章整理——温故而知新,可以为师矣
  19. 尼尔 android,尼尔转生wiki官网版
  20. 射频收发信机架构和射频信号介绍

热门文章

  1. 最强JAVA核心技术群
  2. JQ简单二级导航,加子导航栏
  3. 小系统单据自动生成存储过程
  4. [转载] python 运算符重载有什么用_Python运算符重载用法实例分析
  5. [转载] python中实现矩阵乘法
  6. [转载] python——连接Oracle数据库
  7. 网络编程基础概念-网络协议
  8. Python递归、反射、2分查找、冒泡排序
  9. UVALive - 3641 Leonardo's Notebook(polya计数)
  10. C#数组和集合专题2(Array)