ArrayList source code相关方法阅读
2014-11-21 18:17
435 查看
1、新增一个对象
源码里面elementData对象是这样定义:private transient Object[] elementData;
在之前的开发中从来没用过transient 关键字,特意测试了一下,发现transient就是persistent的对立,当用到字节流、网络传输的时候一旦属性定义为transient将会出现获取值为null的情况。
详情可参考:What is transient keyword in Java?
还有一个需要了解知识点的地方就是Arrays.copyOf(elementData, newCapacity)方法,它是将elementData数组的数据按照newCapacity长度建立一个新的数组的同时,将原数组elementData数组里面的数据拷贝到新的数组中。
2、移除一个对象
这里面关键点就是fastRemove方法中的System.arraycopy(elementData, index+1, elementData, index, numMoved);
这个方法的含义是将elementData数组从index+1位置到numMoved位置的数据拷贝到数组elementData的index位置,依次往后填充
详情可参考:老紫竹
以上两点我们可以看出:list其实的存储其实就是数组;数组都是按位来存储,所以在插入或者是移除的时候都是依次顺序往后推移,所以我们的面试题经常会提到ArrayList查询效率要高于新增和删除的效率
这里暂时只列出两个方法,其他今后有时间研究再这里列出来,待续....
/** * 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查询效率要高于新增和删除的效率
这里暂时只列出两个方法,其他今后有时间研究再这里列出来,待续....
相关文章推荐
- File I/O source code--新建文件 相关方法阅读
- File I/O source code--读取文件 相关方法阅读
- there is no source code available for the current location的解决方法
- CodeModel的范例:一个为项目所有相关类添加工厂方法的AddIn
- 第12周-阅读、修改和运行关于交通工具类的程序,回答问题,以理解相关技术方法
- 源码阅读:简单实现ArrayList中的主要方法
- there is no source code available for the current location的解决方法
- [Delphi]ExpressQuantumGrid_Suite_v5.0_Full_Source_Code_for_Delphi_7 控件包的修正及安装方法
- Microsoft source-code annotation language (SAL) 相关
- ArrayList集合的相关方法
- linux源码阅读利器-GNU GLOBAL Source Code Tag System
- there is no source code available for the current location 解决方法
- ArrayList相关(记录我自己所用到的方法)
- linux kernel source code阅读记录(一)LDS文件详解
- File I/O source code--Pipe 相关方法阅读
- 新浪微博Error_code: 400; Error: 40022:Error: source paramter(appkey) is missing的解决方法
- type sourcecode.glsl for architecture i386问题解决方法
- NetBeans的代码度量插件SourceCodeMetrics的基本使用方法
- iOS9.0-Xcode7-UIWebView加载页面出错解决方法,适用于SDWebImage等一切BitCode和Https相关错误
- hibernate source code 阅读二