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;
}
}

最新文章

  1. 记一次Redis被攻击的事件
  2. 团队项目UML用例图
  3. 一个创建Coco2d-x项目的脚本
  4. tyvj1022 - 进制转换 ——进制为负数
  5. iframe 跨域自适应 纯css解决方法
  6. Linux的五个查找命令 [转]
  7. Delphi自写组件:可设置颜色的按钮(改成BS_OWNERDRAW风格,然后CN_DRAWITEM)
  8. 模板C++ 03图论算法 2最短路之全源最短路(Floyd)
  9. java设计模式--单列模式
  10. 用tar压缩xz格式出错
  11. P2837晚餐队列安排
  12. SAP 优缺点
  13. JSONP方法解决跨域请求
  14. Netty Message RefCount
  15. vs2012添加自定义资源步骤
  16. Maven系列(一)plugin
  17. asp.net MVC 多系统目录结构
  18. Spring NamedParameterJdbcTemplate 详解
  19. jmeter(2)——元件简介、作用域及执行顺序
  20. 谁说接口不能有代码?—— Kotlin接口简介(KAD 26)

热门文章

  1. c#自定义ORM框架---(泛型&amp;反射&amp;实体类扩展属性&lt;附带通用增、删、查、改&gt;)
  2. C头文件中尖括号与双引号的区别及编译搜索顺序
  3. 【Linux】小米路由开启SSH访问权限
  4. 【第三届强网杯】write up
  5. map Codeforces Round #Pi (Div. 2) C. Geometric Progression
  6. Android 性能优化(23)*性能工具之「Heap Viewer, Memory Monitor, Allocation Tracker」Memory Profilers
  7. 初识mybatis之入门案例
  8. [ Luogu 3924 ] 康纳的线段树
  9. poj3411 Paid Roads
  10. Python学习笔记之默认参数