当前位置:网站首页>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);
}
边栏推荐
- 目标检测——ADAS实战
- The linkedhashset set makes the elements orderly without repetition
- Koa quick start
- PMP商业分析概述
- Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter
- Differences between web testing and app testing
- 11 go Foundation: Interface
- Leetcode counts the number of ways to place houses
- Which securities company is the largest and safest? Which securities company has good service
- String substitution
猜你喜欢

Differences between web testing and app testing

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

如何用项目甘特图,做好项目汇报

2022.02.15

Ctfhub web SQL injection - integer injection

Sysbench Pressure Test Oracle (installation and use examples)

目标检测——ADAS实战

MySQL的下载和安装

Relationship between EMC, EMI and EMS

短视频平台常见SQL面试题,你学会了吗?
随机推荐
Which securities company is the largest and safest? Which securities company has good service
Project R & D, what are the free brain mapping tools that are easy to use
Cross border information station
Talk about SQL optimization
Convert flat structure to tree structure
Ctfhub web password weak password
2022.02.15
Regular expression (?: pattern)
Why should the pointer be null after delete
字符串输出
Chrome browser close update Popup
Application of fsockopen function
字符串方法练习
Calculate rectangular area
[redis] data introduction & General Command & string type
leetcode 统计无向图中无法互相到达点对数
The linkedhashset set makes the elements orderly without repetition
Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
月薪没到30K的程序员必须要背的面试八股,我先啃为敬
11 go Foundation: Interface