当前位置:网站首页>List中set方法和add方法的区别
List中set方法和add方法的区别
2022-04-22 05:32:00 【有梦想的攻城狮】
前言
在Java中的常用的集合接口List中有两个非常相似的方法:
E set(int index, E element);
void add(int index, E element);
这两个方法都是在集合的指定位置插入指定的元素,那么这两个方法到底有什么区别呢?接下来我们通过ArrayList这个我们常用集合实现来看一下这两个方法的差异
相同点
首先我们来看一下这两个方法在ArrayList中的相同点
- 他们都会在集合的指定位置插入新的元素,例如下面的例子:
#在集合的第2位插入一个 F
# 通过add方法插入
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
list.add(2, "F");
System.out.println(list);
#运行结果: [A, B, F, C]
#通过set方法插入
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
list.set(2, "F");
System.out.println(list);
#运行结果: [A, B, F]
-
上述两个方法中的element都不允许为null,如果为null的话会抛出NullPointerException
-
上述两个方法中的element对应的类如果无法存入该集合,都会抛出ClassCastException异常
-
上述两个方法中的element对应的类型如果无法存入该集合,都会抛出IllegalArgumentException异常
不同点
- 第一个不同点从上面的例子就可以看出来,add 方法是在指定位置插入新的值,然后将原先位置的元素
和原先位置之后的所有元素都向后移动了一位,而 set 方法是直接覆盖了原来的元素
#在集合的第2位插入一个 F
# 通过add方法插入
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
list.add(2, "F");
System.out.println(list);
#运行结果: [A, B, F, C]
#通过set方法插入
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
list.set(2, "F");
System.out.println(list);
#运行结果: [A, B, F]
- add 方法对于index的取值范围是 0<= index <= size(),也就是说add方法可以在当前集合的最后一个元素后面再添加一个新元素;set 方法对于index的取值范围是 0< index < size(),也就是说set方法只能覆盖当前已有的元素,取值范围不能超出当前集合元素的范围
#通过add在当前集合的第四个位置插入F
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
list.add(3, "F");
System.out.println(list);
#运行结果: [A, B, C, F]
#通过set在当前集合的第四个位置插入F
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
list.set(3, "F");
System.out.println(list);
#运行结果: 抛出NullPointerException
总结
通过总结List接口中上述两个非常相似的常用方法的相同点与不同点,使我们在日常工作中可以更加得心应手的去使用这些常用的集合类
版权声明
本文为[有梦想的攻城狮]所创,转载请带上原文链接,感谢
https://zhangzehai.blog.csdn.net/article/details/124162312
边栏推荐
- Fundamentals of graphics - depth buffer
- Codeforces Round #784 (Div. 4) All Problems
- Fundamentals of graphics - Mobile GPU architecture
- Ezyslice cut characters
- Aiming position of unity shooting game
- DolphinDB VSCode 插件使用教程
- Neural network -- BP neural network
- One click evaluation course script of educational administration management information system
- C language version: establishment and basic operation of chain team
- Integer source code
猜你喜欢

MySQL数据库基础

联机版俄罗斯方块带自动挂机源码

使用easyexcel导出excel表格

AWD platform construction – cardinal

libcurl等第三方库通用编译方法

Neural network -- BP neural network

Codeforces Round #784 (Div. 4) All Problems

C language version: establishment and basic operation of chain team

MySQL installation and configuration - detailed tutorial

用minscript脚本语言实现数学函数曲线编辑器
随机推荐
Fundamentals of graphics - depth of field / DOF
Auto. JS canvas setting anti aliasing paint setAntiAlias(true);
GBase 8s V8.8 SQL 指南:教程-5.3
GBase 8s V8. 8 SQL Guide: tutorial-5.4
unity接入ILRuntime之后 热更工程应用Packages下面的包方法 例如Unity.RenderPipelines.Core.Runtime包
Integer source code
Online Tetris with automatic hang-up source code
[candelastudio edit CDD] - 2.3 - realize the jump between multiple securitylevels of $27 service (UDS diagnosis)
有限单元法基本原理和数值方法_有限元法基本原理
New tips for JS in 2022
联机版俄罗斯方块带自动挂机源码
GBase 8s V8.8 SQL 指南:教程-5.4
用minscript脚本语言实现数学函数曲线编辑器
Using easyexcel to export excel forms
Learning notes - longest subsequence, largest sub block
Redis real-time synchronization tool recommendation
Hloj 1936 covered with squares
Codeforces Round #783 (Div. 2) ABCD
Access denied for user ‘root‘@‘% mysql在问题(解决)
[fedmd, a heterogeneous FL training method using model distillation] fedmd: heterogeneous federated learning via model distillation