ArrayList (数组链表)使用Object数组作为存储。

    /**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData; /**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};

  

默认构造函数为:

    /**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}

 构造一个空的链表,该链表没有进行初始化,在调用其add()方法时,进行了初始化:

    /**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

 ensureCapacityInternal()方法是

    private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
} ensureExplicitCapacity(minCapacity);
} private void ensureExplicitCapacity(int minCapacity) {
modCount++; // overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
} /**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
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()方法:

    /**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
rangeCheck(index); return elementData(index);
} @SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}

   

  

最新文章

  1. 【Java每日一题】20161116
  2. QQ授权登录
  3. 每日一练(写不出心得体会了!毕竟哪有那么多心得好写。然后看github上有很多不错的题目。分享一下!)
  4. 织梦DedeCMS子目录移动到根目录的方法
  5. [MVC4-基礎] 使用DataAnnotations+jQuery進行表單驗證
  6. Git远程使用技巧
  7. Mac OSX下面的博客客户端Marsedit使用
  8. ARCH和LGWR进程同步DG日志的区别
  9. pig的一些实例(我常用的语法)
  10. es6中的...三个点
  11. css td hover 选择器无效
  12. 《Lua程序设计》第7章 迭代器与泛型for 学习笔记
  13. PyQt5 笔记(02):嵌套布局
  14. jquery中的$().each和$.each的区别
  15. Ajax-08 跨域获取最新电视节目清单实例
  16. C++练习--实现客户机(CLIENT)类
  17. vue2.0中改变了数组值不能实时反映到页面
  18. turbolink 造成 link_to异常
  19. js基础的自定义属性练习
  20. 模仿手机qq空间头部向上滚动颜色加深

热门文章

  1. nexus、maven私服仓库(一)
  2. svn图标的含义
  3. html-选择对象
  4. 001. Ansible简介
  5. linux 硬盘分区与格式化挂载
  6. Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
  7. Java基础(十三) 文件高级技术
  8. [三边定位] C# 演示程序
  9. tarjan求双联通分量(割点,割边)
  10. 变量安全过滤,防止xss攻击