当前位置:网站首页>ArrayList basic operation and add element source code
ArrayList basic operation and add element source code
2022-06-29 02:38:00 【Why not sell egg cakes well】
ArrayList increase
List Implementation class of interface .
Array is used at the bottom
High query efficiency , The efficiency of addition and deletion is low , unsafe .
List<String> list=new ArrayList<>();
// Additive elements
list.add("bjsxt");
list.add("puty");
// Note that the array index of the following addition method should not be greater than the number of existing elements
list.add(3,"jlhh");// An error at this time
Get elements
for(int i=0;i<list.size(),i++){
sout(list.get(i));
}
Remove elements
Deletes the specified element according to the index
The return value is the value of the deleted element
String value=list.remove(1);
Replacement elements
String val=list.set(0,"ibts");
Empty the container
list.clear();
Judge whether the container is empty
list.isEmpty();
Whether to include the specified element
boolean flag=list.contains("xiaozhang");
I feel very bored here , Just skip something .
ArrayList Bottom source
AbstractList
private static final int DEFAULT_CAPACITY=10;
transienet Object[] elementData;
trasient Data persistence Will not be saved to disk
private int size;
public ArrayList(){
this.elementData=DEFAULTCAPACITY_EMPTY_ELEMENTDATA; //{}
}
jdk1.7 Immediately load
1.8 Delay loading Empty array When to use and when to change the length of the array , Rational use of space .
add Method
The core is the last grow Method , Determine when to expand the array .
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);
}
边栏推荐
- Ctfhub web SQL injection - integer injection
- String attribute exercise
- “内窥镜第一股”二闯IPO,去年亏损5个亿,核心产品商业化仍存疑 | IPO速递
- Some tests on complementary wasm environment
- 11 go Foundation: Interface
- Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
- Three methods of time series prediction: statistical model, machine learning and recurrent neural network
- mgalcu-a509
- leetcode 统计无向图中无法互相到达点对数
- B1009 irony
猜你喜欢

Target detection - ADAS practice

PWN攻防世界Level2

Have you learned the common SQL interview questions on the short video platform?

Sysbench Pressure Test Oracle (installation and use examples)

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin

CTFHub-Web-SQL注入-整数型注入

pvcreate asm disk导致asm磁盘组异常恢复---惜分飞

高并发的理解与设计方案

Download and installation of MySQL

三角函数计算
随机推荐
短视频平台常见SQL面试题,你学会了吗?
String length
The meaning of cross multiplication and dot multiplication (simple formula memory method)
项目研发,有哪些好用的免费脑图工具软件
Handling method of occasional error reporting on overseas equipment
Use code binding DataGridView control to display tables in program interface
计算矩形面积
【无标题】
字符串输出
pvcreate asm disk导致asm磁盘组异常恢复---惜分飞
MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year
安装mysql5.7 并修改密码
Talk about the copyonwritearraylist of JUC
PHP XML expat parser
Sysbench Pressure Test Oracle (installation and use examples)
Regular expression (?: pattern)
Mipi d-phy -- contents of HS and LP agreements
String attribute exercise
Temperature conversion II
MySQL的下载和安装