Java Collections Framework之ArrayList源码分析

openkk 8年前
     该类是JDK1.2中添加的类,可用于替代Vector(1.1中包含的类库) 其内部持有的也是一个数组。Hierarchy的结构如下:    <br />    <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/d6fa870d804da422cab75d37f87c422e.jpg" width="310" height="289" />    <br /> 该类并非线程安全的,主要含有两个字段:    <br />     elementData:内部持有的数组    <br />    <p>    size:ArrayList的长度</p>    <p>    构造函数如下:<br />     <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/1d41a115b9ae5296068a7c70d29a2751.jpg" width="544" height="123" /><br /> 利用该参数初始化该数组。<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/5c2359b2bb7d6b03a656df9bea22ade2.jpg" width="296" height="70" /><br /> 将数组的大小设置为10,为该数组分配内存空间。<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/755ffad165e5c82e830d9c01cd0b02a1.jpg" width="554" height="134" /><br /> 根据Collection c初始化该ArrayList,底层使用的是System.arrayCopy方法 </p>    <p> 向该List的尾部添加元素:<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/3ff9b7554fc6d2e67d1a7b3c2ba0a406.jpg" width="432" height="103" /><br /> 首先会调用ensureCapacity(size + 1)方法,检查该List持有的数组是否已到达峰值,如果到了则扩充此数组<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/4039023dc66c76e28e3021b1f801c7e5.jpg" width="541" height="209" /><br /> 由代码可知,每次扩充为原来的一点五倍,可知该操作当容量变大的时候会严重造成内存空间的浪费。</p>    <p> addAll(int index, Collection     <!--? extends E--> c):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/6afa704996ab357c6609c08ca22679bb.jpg" width="586" height="321" /><br /> 在index位置处依次添加c中的元素,同时将该index处的元素依次右移。</p>    <p> addAll(Collection     <!--? extends E--> c):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/f3ede5f8cf957f8483ff48bdbce4c1c7.jpg" width="462" height="144" /><br /> 从该List的尾部依次添加c中含有的元素</p>    <p>clear():<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/6972a129222de66cfeee8811ab1b39e3.jpg" width="295" height="163" /><br /> 清空该List,内部持有的数组的长度并不发生变化。</p>    <p>indexOf(Object o):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/73bcd349a7893b499e37c6e397e18faa.jpg" width="317" height="226" /><br /> 返回某元素在该List中的索引。</p>    <p>indexOf(Object o):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/b81b0ca807344f59b81aa1f196a78f96.jpg" width="322" height="204" /><br /> 从该List尾部查找第一次出现该o时的索引。</p>    <p>contains(Object o):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/8b11404901bf9b9629715f0ca5534822.jpg" width="345" height="59" /><br /> 查找该List是否包含o</p>    <p>fastRemove(int index):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/3e5da085a006de164039ae5613fffb0a.jpg" width="581" height="155" /><br /> 删除该索引处的元素,并且使该索引后面的元素依次左移。</p>    <p>get(int index):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/fa8852d22a8755bad86bf25d5cabf9ff.jpg" width="300" height="93" /><br /> 获取索引index处的元素。</p>    <p>isEmpty():<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/5cd0183f973b89a850274e65ea4669a6.jpg" width="307" height="65" /><br /> 判断该List是否为空</p>    <p>remove(int index):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/8b544bd46dfe854ba34b346142067800.jpg" width="525" height="256" /><br /> 删除index处的元素,同时右边的元素左移。</p>    <p>remove(Object o):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/9edef7059a62bdb7583c7643b81ec52f.jpg" width="467" height="293" /><br /> 先查找,再删除。设计到数组元素的左移。</p>    <p>set(int index, E element):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/d95263cbe6416d4e63f6df229683de71.jpg" width="311" height="125" /><br /> 于位置index处设置为element</p>    <p>size():<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/de8ca86546daa00ec7bff55468c6a544.jpg" width="194" height="62" /><br /> 返回该集合的长度</p>    <p>toArray():<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/5fa929f1954c75ce1218f765333e7ea5.jpg" width="381" height="70" /><br /> 将该集合转化为Object数组。</p>    <p>toArray(T[] a):<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/3859727a9bc5f81952617db782bd1826.jpg" width="561" height="171" /><br /> 将该集合转化为特定的数组。</p>    <p>trimToSize():<br /> <img alt="Java Collections Framework之ArrayList源码分析" src="https://simg.open-open.com/show/614f2ed651ad083d6c36d7222a38bc07.jpg" width="453" height="126" /><br /> 将该数组中空的占位符剔除,以便GC回收。</p>    <p>总结:<br />       该类和Vector的机构几乎完全相同,<span style="color:#e53333;">该集合非线程安全</span>。</p>