ArrayList source code相关方法阅读
来源:互联网 发布:mac苹果手机铃声设置 编辑:程序博客网 时间:2024/06/03 20:59
1、新增一个对象
/** * 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) {ensureCapacity(size + 1); // Increments modCount!!elementData[size++] = e;return true; }
/** * Increases the capacity of this <tt>ArrayList</tt> instance, if * necessary, to ensure that it can hold at least the number of elements * specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ public void ensureCapacity(int minCapacity) {modCount++;int oldCapacity = elementData.length;if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity)newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity);} }
源码里面elementData对象是这样定义:private transient Object[] elementData;
在之前的开发中从来没用过transient 关键字,特意测试了一下,发现transient就是persistent的对立,当用到字节流、网络传输的时候一旦属性定义为transient将会出现获取值为null的情况。
详情可参考:What is transient keyword in Java?
还有一个需要了解知识点的地方就是Arrays.copyOf(elementData, newCapacity)方法,它是将elementData数组的数据按照newCapacity长度建立一个新的数组的同时,将原数组elementData数组里面的数据拷贝到新的数组中。
2、移除一个对象
/** * Removes the first occurrence of the specified element from this list, * if it is present. If the list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns <tt>true</tt> if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return <tt>true</tt> if this list contained the specified element */ public boolean remove(Object o) {if (o == null) { for (int index = 0; index < size; index++)if (elementData[index] == null) { fastRemove(index); return true;}} else { for (int index = 0; index < size; index++)if (o.equals(elementData[index])) { fastRemove(index); return true;} }return false; }
/* * Private remove method that skips bounds checking and does not * return the value removed. */ private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work }
这里面关键点就是fastRemove方法中的System.arraycopy(elementData, index+1, elementData, index, numMoved);
这个方法的含义是将elementData数组从index+1位置到numMoved位置的数据拷贝到数组elementData的index位置,依次往后填充
详情可参考:老紫竹
以上两点我们可以看出:list其实的存储其实就是数组;数组都是按位来存储,所以在插入或者是移除的时候都是依次顺序往后推移,所以我们的面试题经常会提到ArrayList查询效率要高于新增和删除的效率
这里暂时只列出两个方法,其他今后有时间研究再这里列出来,待续....
0 0
- ArrayList source code相关方法阅读
- File I/O source code--新建文件 相关方法阅读
- File I/O source code--读取文件 相关方法阅读
- File I/O source code--写入文件 相关方法阅读
- File I/O source code--Pipe 相关方法阅读
- HashMap source code 阅读
- hibernate source code 阅读一
- hibernate source code 阅读二
- hibernate source code 阅读三
- Microsoft source-code annotation language (SAL) 相关
- ArrayList集合的相关方法
- linux源码阅读利器-GNU GLOBAL Source Code Tag System
- 源码阅读利器-GNU GLOBAL Source Code Tag System
- FSE 2017 Understanding Misunderstandings in Source Code 阅读笔记
- 【Android】关联source code的方法
- source code
- source code
- source code
- FQA android----界面布局的颜色和Xml定义的不一致
- 初识listview,写个小demo
- inpaint小工具debug--完工咯!
- log4j:ERROR setFile(null,true) call failed.
- LeetCode——Maximum Product Subarray
- ArrayList source code相关方法阅读
- eclipse中导入文件到模拟器
- hdu1272小希的迷宫(并查集)
- chrome扩展开发手记<1>-需求分析
- flash打印指定页码的报表
- 在MyEclipse中使用Spring,跨出Spring伟大的一步
- C++中智能指针的设计和使用
- UVa做题阶段性总结(2014-11-21)
- asdasdasd