java 源码分析2 -List
1.是一个接口,继承了Collection,提供了size(),isEmpty(),contanis(),iterator(),toArray(),clear()等方法
2.分析常用的ArrayList,LinkedList,set等
3. ArrayList 是一个对象数组
//被transient 定义的不进行序列化
private transient Object[] elementData;
b add 方法
public boolean add(E e) {
//扩容操作,通过sytem.copy 来复制
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//num >> 1,相当于num除以2,每次增加原数组长度的二分之一
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
// get 方法
public E get(int index) {
//检查边界
rangeCheck(index);
//取出下标对应的值
return elementData(index);
}
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
// 把后面的元素全部移动,通过复制算法来覆盖实现
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
public void clear() {
modCount++;
// Let gc do its work
// 把所有元素值置为null,size为0
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
3 LinkedList 本质是
//一个单向链表 transient Node<E> first; /**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
//把最后一个元素赋值给l
final Node<E> l = last;
//新建一个节点,上一个元素为l
final Node<E> newNode = new Node<>(l, e, null);
//最后一个节点为当前节点
last = newNode;
if (l == null)
//第一个节点为当前节点
first = newNode;
else
//l指向当前节点
l.next = newNode;
size++;
modCount++;
}
public E get(int index) {
//检查
checkElementIndex(index);
//取值
return node(index).item;
}
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
最新文章
- 记一次Redis被攻击的事件
- 团队项目UML用例图
- 一个创建Coco2d-x项目的脚本
- tyvj1022 - 进制转换 ——进制为负数
- iframe 跨域自适应 纯css解决方法
- Linux的五个查找命令 [转]
- Delphi自写组件:可设置颜色的按钮(改成BS_OWNERDRAW风格,然后CN_DRAWITEM)
- 模板C++ 03图论算法 2最短路之全源最短路(Floyd)
- java设计模式--单列模式
- 用tar压缩xz格式出错
- P2837晚餐队列安排
- SAP 优缺点
- JSONP方法解决跨域请求
- Netty Message RefCount
- vs2012添加自定义资源步骤
- Maven系列(一)plugin
- asp.net MVC 多系统目录结构
- Spring NamedParameterJdbcTemplate 详解
- jmeter(2)——元件简介、作用域及执行顺序
- 谁说接口不能有代码?—— Kotlin接口简介(KAD 26)
热门文章
- c#自定义ORM框架---(泛型&;反射&;实体类扩展属性<;附带通用增、删、查、改>;)
- C头文件中尖括号与双引号的区别及编译搜索顺序
- 【Linux】小米路由开启SSH访问权限
- 【第三届强网杯】write up
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
- Android 性能优化(23)*性能工具之「Heap Viewer, Memory Monitor, Allocation Tracker」Memory Profilers
- 初识mybatis之入门案例
- [ Luogu 3924 ] 康纳的线段树
- poj3411 Paid Roads
- Python学习笔记之默认参数