List接口的介绍
- java.util.List接口继承自Collection接口,是单列集合的一个重要分支,习惯性地会将实现了List接口的对象称为List集合。
- 在List集合中允许出现重复的元素,所有的元素是以一种线性方式进行存储的,在程序中可以通过索引来访可集合中的指定元素。
- 另外,List集合还有一个特点就是元素有序,即元素的存入顺序和取出顺序一致。
List接口的特点
- 它是一个元素存取有序的集合。例如,存元素的顺序是11、22、33。那么集合中,元素的存储就是按照11、22、33的顺序完成的。
- 它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。
- 集合中可以有重复的元素,通过元素的 equals方法,来比较是否为重复的元素。
List接口中带索引的(特有)方法
复制 | |
| public void add(int index, E element) |
| |
| |
| public E get(int index) |
| |
| |
| public E remove(int index) |
| |
| |
| public E set(int index, E element) |
add()方法
复制 | 说明: |
| hasNext()方法,获取迭代器是否含有下一个元素(含有就返回true) |
| next()方法,获取迭代器下一个元素 |
复制 | import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| public class DemoListAdd { |
| public static void main(String[] args) { |
| |
| List<String> arrayList = new ArrayList<>(); |
| |
| |
| arrayList.add(0, "Index 0 元素"); |
| arrayList.add(1, "Index 1 元素"); |
| arrayList.add(2, "Index 2 元素"); |
| |
| |
| |
| Iterator<String> ite = arrayList.iterator(); |
| |
| while (ite.hasNext()) { |
| System.out.println( |
| ite.next() |
| ); |
| } |
| } |
| } |
复制 | 输出结果: |
| Index 0 元素 |
| Index 1 元素 |
| Index 2 元素 |
get()方法
复制 | import java.util.ArrayList; |
| import java.util.List; |
| |
| public class DemoListGet { |
| public static void main(String[] args) { |
| |
| List<String> arrayList = new ArrayList<>(); |
| |
| |
| arrayList.add(0, "Index 0 元素"); |
| arrayList.add(1, "Index 1 元素"); |
| arrayList.add(2, "Index 2 元素"); |
| |
| |
| String index0 = arrayList.get(0); |
| String index1 = arrayList.get(1); |
| String index2 = arrayList.get(2); |
| |
| |
| System.out.println("索引0处的元素:" + index0); |
| System.out.println("索引1处的元素:" + index1); |
| System.out.println("索引2处的元素:" + index2); |
| } |
| } |
复制 | 输出结果: |
| 索引0处的元素:Index 0 元素 |
| 索引1处的元素:Index 1 元素 |
| 索引2处的元素:Index 2 元素 |
remove()方法
复制 | public class DemoListRemove { |
| public static void main(String[] args) { |
| |
| List<String> arrayList = new ArrayList<>(); |
| |
| |
| arrayList.add(0, "元素0"); |
| arrayList.add(1, "元素1"); |
| arrayList.add(2, "元素2"); |
| |
| System.out.println("移除元素前:" + arrayList); |
| |
| |
| arrayList.remove(1); |
| System.out.println("移除元素1后:" + arrayList); |
| } |
| } |
复制 | 输出结果: |
| 移除元素前:[元素0, 元素1, 元素2] |
| 移除元素1后:[元素0, 元素2] |
注意:移除一个元素以后,在被移除元素的后面的每个元素索引减1
set()方法
复制 | import java.util.ArrayList; |
| import java.util.List; |
| |
| public class DemoListSet { |
| public static void main(String[] args) { |
| |
| List<String> arrayList = new ArrayList<>(); |
| |
| |
| arrayList.add(0, "原始元素0"); |
| arrayList.add(1, "原始元素1"); |
| arrayList.add(2, "原始元素2"); |
| |
| System.out.println("集合被替换元素前:" + arrayList); |
| |
| |
| arrayList.set(0, "替换元素0"); |
| System.out.println("集合被替换元素后:" + arrayList); |
| } |
| } |
复制 | 输出结果: |
| 集合被替换元素前:[原始元素0, 原始元素1, 原始元素2] |
| 集合被替换元素后:[替换元素0, 原始元素1, 原始元素2] |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)