java集合
ArrayList
构造函数
- 空参 、带参、带数组
- 空数组、参数检测、 集合copyof
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20private static final Object[] DEFAULTCAPACITY_EMPTY_EMEMENT = {};
//1空参
public ArrayList(){
this.elementData = DEFAULTCAPACITY_EMPTY_EMEMENT;
}
//2带参数
public ArrayList(int initialCapacity){
//1. 该参数 > 0 this.elementData = new Object[initialCapacity];
//2. 参数 = 0 this.elementData = DEFAULTCAPACITY_EMPTY_EMEMENT;
//3.else 抛异常: illegalArgumentException
}
// 传递集合
public ArrayList(Collection<? extends E> c){
//1.内容变数组 : elementData = c.toArray();
//2/数组长度 >=0 且非object类型 全部复制过来
elementData = Arrays.copyOf(elementData,size,Object[].class);
//3.如果是空数组 复制空
}插入数据
- index范围检测 / 是否扩容
- 数组后移
- 插入数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24//1.直接插入
public boolean add(E e){
//检测是否需要扩容 ensureCapacityInternal(int minCapacity)
ensureCapacityInternal(size+1);
/** 判断条件:if(minCapacity - elementData.length > 0) 当前长度+1 - 总长 > 0
* 调用方法:grow(minCapacity)
* oldCapacity = elementData.length;
* newCapacity = oldCapacity + (oldCapacity >> 1) 扩容:增加1.5倍
*/
elementData[size++] = e;
return true;
}
//2.序列index + e 插入 指定位置放 如果存在 整个数据后移
public void add(int index, E element){
//1.index范围检测 rangeCheckForAdd(index)
//2.检测扩容
//3.数组往后移1位
// 数组a ,下标, 数组a , 下标+1, 长度
System.arraycopy(elementData,index, elementData,index+1, size - index );
//4.插入index元素 size++
elementData[index] = element;
size++;
}
扩容详解
总结:size转为安全size + 进行1.5倍扩容
- 扩容入口
- 计算最小size (空数组:size)
- 是否符合扩容条件
- 扩容copyof
1
2
3
4
5
6
7
8
9
10
11
12
13ensureCapacityInteral(int minCapacity) //1.扩容入口(当前size+1)
-> ensureExplicitCapacity( calculateCapacity(elementData, minCapacity));
//2.计算最小容量 =>(数组 + 当前size)
// 空数组就给10 不空 给size
calculateCapacity(Object[] elementData, int minCapacity)
{ //1.elementData 是否为空 y:Math.max(默认容量:10, minCapacity)
// 不是空的 n:return minCapacity}
//3.查看是否符合扩容条件 -》grow()
ensureExplicitCapacity(int minCapacity){
minCapacity - elementData.lenth > 0 ? grow(minCapacity:扩容):return;(不扩容)
}
//4.扩容1.5倍 + 溢出异常
private void grow(){ elementData = Arrays.copyof(elementData,newCapacity);}
删除方法
- 指定位置删除、 指定Object删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14public E remove(int index){
//1.检测index值
//2.返回要删除的元素 E oldValue = elementData(index);
//3.现在移动长度 + 移动
numMoved = size - index - 1;
if(numMoved > 0)
System.arrayCopy(elementData,index+1, elementData,index, numMoved);
//4.删除
elementData[--size] = null;
}
public boolean remove(Object o){
//1.元素是否为空 找到 elementData[index] == null -> fastRemove(index) ——fastRemove删除操作如上
// 不为空 o.equals(elementData[index]) -> fastRemove(index)
}异常处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21for(Integer num : list){
if(num == 112)
list.remove(num);
}
//异常Exception java.util.ConcurrentModificationException
// -> 迭代器
public E next(){
checkForComodification(); //集合遍历的时候 集合的大小改变了 导致删除时报错
// if(modCount != expectedModCount)
// throw new ConcurrentModificationException();
if(i >= elementData.length)
throw new ConcurrentModificationException();
}
//改变 使用迭代器
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
Integer num = it.next();
if(num == 12)
it.remove();
}
6.