当前位置:网站首页>arraylist基操和添加元素源码
arraylist基操和添加元素源码
2022-06-29 02:31:00 【为什么不好好卖蛋饼】
ArrayList增加
List接口的实现类。
底层使用数组
查询效率高,增删效率低,不安全。
List<String> list=new ArrayList<>();
//添加元素
list.add("bjsxt");
list.add("puty");
//注意下面的添加方式数组索引不得大于现有元素个数
list.add(3,"jlhh");//此时报错
获取元素
for(int i=0;i<list.size(),i++){
sout(list.get(i));
}
删除元素
根据索引删除指定元素
返回值是被删除的元素的值
String value=list.remove(1);
替换元素
String val=list.set(0,"ibts");
清空容器
list.clear();
判断容器是否空
list.isEmpty();
是否包含指定元素
boolean flag=list.contains("xiaozhang");
到这里感觉特别无聊,直接跳过了一些东西。
ArrayList底层源码
AbstractList
private static final int DEFAULT_CAPACITY=10;
transienet Object[] elementData;
trasient 数据持久化 不会被保存磁盘
private int size;
public ArrayList(){
this.elementData=DEFAULTCAPACITY_EMPTY_ELEMENTDATA; //{}
}
jdk1.7 立即加载
1.8 延迟加载 给空数组 什么时候用什么时候改变数组长度,合理使用空间。
add方法
核心是最后的grow方法,判断何时应该对数组做扩容。
public boolean add(E e){
ensureCapacityInternal(size+1);
elementData[size++]=e;
return true;
}
private void ensureCapacityInternal (int minCapacity){
ensureExplicitCapacity(calculateCapacity(elementData,minCapacity))
}
private static int calculateCapacity(Object[] elementData,int minCapacity){
if(elementData==DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
return Math.max(DEFAULT_CAPACITY,minCapacity);
}
return minCapacity;
}
private void ensureExpricitCapacity(int minCapacity){
modCount++;
if(minCapacity-elementData.length>0){
grow(minCapacity);
}
}
private void grow(int minCapacity){
int oldCapacity=elementData.length;
int newCapacity=oldCapacity+(oldCapacity>>1);
if(newCapacity-minCapacity<0){
newCapacity=minCapacity;
}
if(newCapacity-MAX_ARRAY_SIZE>0){
newCapacity=hugeCapacity(minCapacity);
}
elementData=Arrays.copyOf(elementData,newCapacity);
}
边栏推荐
- PMP商业分析概述
- [redis] data introduction & General Command & string type
- 字符串长度
- MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year
- CTFHub-Web-密码口令-默认口令
- 学习太极创客 — MQTT 第二章(九)本章测试
- PHP database ODBC
- 安装kibana
- 信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
- 如何用项目甘特图,做好项目汇报
猜你喜欢

Sysbench Pressure Test Oracle (installation and use examples)
![[redis] data introduction & General Command & string type](/img/86/3abc5047f9c0a051f432e82ccc816c.png)
[redis] data introduction & General Command & string type

Target detection - ADAS practice

The 10 most commonly used gadgets for waterfall project management can be built and used freely

leetcode 统计放置房子的方式数

干货丨微服务架构是什么?有哪些优点和不足?

Handling method of occasional error reporting on overseas equipment

Ctfhub web SQL injection - integer injection

Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first

MySQL的下载和安装
随机推荐
方法重载小结
Studies of relative costs for development in different languages
Eliminate the hover effect when the button is disabled
String output
Temperature conversion II
The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
Informatics Olympiad 1361: Produce
信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
Download and installation of MySQL
Koa 快速入門
SystemVerilog structure (I)
PHP SimpleXML
Day10 enumeration class and annotation
Pyinstaller打包pikepdf失败的问题排查
Project R & D, what are the free brain mapping tools that are easy to use
50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
What is the Valentine's Day gift given by the operator to the product?
【无标题】
【無標題】
Handling method of occasional error reporting on overseas equipment