1 /*

2 继承自AbstractList,实现了List、RandomAccess、Cloneable、Serializable接口3 1)RandomAccess接口:用来快速随机存取,在实现了该接口后,用普通for来遍历,性能更高4 2)Cloneable接口:实现了该接口,就可以使用Object.Clone()方法了5 3)Serializable接口:实现了该接口,表明该类可以被序列化6 */

7 public class ArrayList extends AbstractList

8 implements List, RandomAccess, Cloneable, java.io.Serializable9 {10 //版本号

11 private static final long serialVersionUID = 8683452581122892189L;12 //缺省容量

13 private static final int DEFAULT_CAPACITY = 10;14 //缺省空对象数组

15 private static final Object[] EMPTY_ELEMENTDATA ={};16 //默认大小的对象数组

17 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA ={};18 //元素数组

19 transientObject[] elementData;20 //数组的大小

21 private intsize;22

23 //构造方法24 //无参构造 Constructs an empty list with an initial capacity of ten

25 publicArrayList() {26 this.elementData =DEFAULTCAPACITY_EMPTY_ELEMENTDATA;27 }28

29 /**

30 * Constructs an empty list with the specified initial capacity.31 *32 *@paraminitialCapacity the initial capacity of the list33 *@throwsIllegalArgumentException if the specified initial capacity34 * is negative35 */

36 public ArrayList(intinitialCapacity) {37 if (initialCapacity > 0) {38 this.elementData = newObject[initialCapacity];39 } else if (initialCapacity == 0) {40 this.elementData =EMPTY_ELEMENTDATA;41 } else{42 throw new IllegalArgumentException("Illegal Capacity: "+

43 initialCapacity);44 }45 }46

47 /**

48 * Constructs a list containing the elements of the specified49 * collection, in the order they are returned by the collection's50 * iterator.51 *52 *@paramc the collection whose elements are to be placed into this list53 *@throwsNullPointerException if the specified collection is null54 */

55 public ArrayList(Collection extends E>c) {56 elementData = c.toArray(); //转换为数组

57 if ((size = elementData.length) != 0) { //数组长度大于058 //c.toArray might (incorrectly) not return Object[] (see 6260652)

59 if (elementData.getClass() != Object[].class)60 elementData = Arrays.copyOf(elementData, size, Object[].class);61 } else{62 //replace with empty array.

63 this.elementData =EMPTY_ELEMENTDATA;64 }65 }66 //总体而言:arrayList的构造方法就是做一件事,初始化一下储存数据的容器,67 //其实本质就是一个数组,在其中就叫elementData68

69 //核心方法

70 /**

71 * Appends the specified element to the end of this list.72 * 默认直接在末尾添加元素73 *74 *@parame element to be appended to this list75 *@returntrue (as specified by {@linkCollection#add})76 */

77 public boolean add(E e) { //elementData=[1,2,3] e=678 //确保数组的容量足够

79 ensureCapacityInternal(size + 1); //Increments modCount!!80 //数组尾部添加元素e

81 elementData[size++] =e;82 return true;83 }84

85 private void ensureCapacityInternal(int minCapacity) { //minCapacity=4

87 ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));88 }89

90 //计算容量

91 private static int calculateCapacity(Object[] elementData, int minCapacity) {//elementData=[1,2,3],minCapacity=492 //如果元素数组等于默认容量的元素数组,就返回DEFAULT_CAPACITY和minCapacity两者的最大值

93 if (elementData ==DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {94 returnMath.max(DEFAULT_CAPACITY, minCapacity);95 }96 //否则就返回minCapacity

97 return minCapacity; //4

98 }99

100 private void ensureExplicitCapacity(int minCapacity) { //minCapacity=4

101 modCount++;102

103 //overflow-conscious code

104 /*

105 这里有两种情况:106 情况一:elementData为空数组,minCapacity=size+1=1,1-0>0,调用grow()进行扩容107 情况二:elementData不为空数组,minCapacity=size+1,为elementData原始的数组(未调用add方法前),108 如果length不够用,再调用grow()进行扩容109 */

110 if (minCapacity - elementData.length > 0)111 grow(minCapacity);112 }113

114 /**

115 * 增加容量以确保它至少可以容纳由最小容量参数指定的元素数116 * Increases the capacity to ensure that it can hold at least the117 * number of elements specified by the minimum capacity argument.118 *119 *@paramminCapacity the desired minimum capacity120 */

121 private void grow(intminCapacity) {122 //overflow-conscious code123 //将原始的elementData.length赋给oldCapacity

124 int oldCapacity =elementData.length;125 //新容量=1.5*oldCapacity

126 int newCapacity = oldCapacity + (oldCapacity >> 1);127 //如果elementData为空数组,那么oldCapacity和newCapacity都为0,将newCapacity直接赋值为10

128 if (newCapacity - minCapacity < 0)129 newCapacity =minCapacity;130 //如果newCapacity超过了最大的容量限制,将调用hugeCapacity()做进一步处理

131 if (newCapacity - MAX_ARRAY_SIZE > 0)132 newCapacity =hugeCapacity(minCapacity);133 //minCapacity is usually close to size, so this is a win:134 //newCapacity确定好了以后,就copyof数组,进行扩容

135 elementData =Arrays.copyOf(elementData, newCapacity);136 }137

138 private static int hugeCapacity(intminCapacity) {139 if (minCapacity < 0) //overflow

140 throw newOutOfMemoryError();141 //判断minCapacity是否大于最大的数组容量MAX_ARRAY_SIZE:是,返回Integer.MAX_VALUE(2147483647);142 //否则返回MAX_ARRAY_SIZE

143 return (minCapacity > MAX_ARRAY_SIZE) ?

144 Integer.MAX_VALUE :145 MAX_ARRAY_SIZE;146 }147

148 /**

149 * 在指定位置插入元素150 * Inserts the specified element at the specified position in this151 * list. Shifts the element currently at that position (if any) and152 * any subsequent elements to the right (adds one to their indices).153 *154 *@paramindex index at which the specified element is to be inserted155 *@paramelement element to be inserted156 *@throwsIndexOutOfBoundsException {@inheritDoc}157 */

158 public void add(intindex, E element) {159 //检查插入的位置是否合理

160 rangeCheckForAdd(index);161

162 //插入元素之后,将index之后的元素都往后移一位

163 ensureCapacityInternal(size + 1); //Increments modCount!!

164 System.arraycopy(elementData, index, elementData, index + 1,165 size -index);166 elementData[index] =element;167 size++;168 }169

170 /**

171 * 用于add和addAll方法的范围检查172 * A version of rangeCheck used by add and addAll.173 */

174 private void rangeCheckForAdd(intindex) {175 if (index > size || index < 0)176 throw newIndexOutOfBoundsException(outOfBoundsMsg(index));177 }178

179 /**

180 * 删除指定位置的元素181 * Removes the element at the specified position in this list.182 * Shifts any subsequent elements to the left (subtracts one from their183 * indices).184 *185 *@paramindex the index of the element to be removed186 *@returnthe element that was removed from the list187 *@throwsIndexOutOfBoundsException {@inheritDoc}188 */

189 public E remove(intindex) {190 //范围检查

191 rangeCheck(index);192

193 modCount++;194 E oldValue = elementData(index); //要删除指定index位置的值195

196 //计算要移动的位数

197 int numMoved = size - index - 1;198 if (numMoved > 0)199 /*

200 public static native void arraycopy(Object src, int srcPos,201 Object dest, int destPos,202 int length);203 Copies an array from the specified source array, beginning at the204 specified position, to the specified position of the destination array.205 从源数组的特殊位置开始拷贝到目标数组的特定位置206

207 System.arraycopy(elementData, index+1, elementData, index,208 size - index - 1); //size=8,index=5,8-5-1=2,src=[1,2,3,4,5,7,8]209 */

210 System.arraycopy(elementData, index+1, elementData, index,211 numMoved);212 elementData[--size] = null; //clear to let GC do its work

213

214 returnoldValue;215 }216

217 /**

218 * 移除list列表中第一次出现的待删除的元素219 * Removes the first occurrence of the specified element from this list,220 * if it is present. If the list does not contain the element, it is221 * unchanged. More formally, removes the element with the lowest index222 * i such that223 * (o==null ? get(i)==null : o.equals(get(i)))224 * (if such an element exists). Returns true if this list225 * contained the specified element (or equivalently, if this list226 * changed as a result of the call).227 *228 *@paramo element to be removed from this list, if present229 *@returntrue if this list contained the specified element230 */

231 public booleanremove(Object o) {232 //依次遍历删除

233 if (o == null) {234 for (int index = 0; index < size; index++)235 if (elementData[index] == null) {236 fastRemove(index);237 return true;238 }239 } else{240 for (int index = 0; index < size; index++)241 if(o.equals(elementData[index])) {242 fastRemove(index);243 return true;244 }245 }246 return false;247 }248 //remove函数用户移除指定下标的元素,此时会把指定下标到数组末尾的元素向前移动一个单位,249 //并把最后一个元素置为null

250

251 /**

252 * 将list表中指定位置的元素替换掉253 * Replaces the element at the specified position in this list with254 * the specified element.255 *256 *@paramindex index of the element to replace257 *@paramelement element to be stored at the specified position258 *@returnthe element previously at the specified position259 *@throwsIndexOutOfBoundsException {@inheritDoc}260 */

261 public E set(intindex, E element) {262 //范围检查

263 rangeCheck(index);264

265 //指定位置的元素值

266 E oldValue =elementData(index);267 //将elementData的指定位置替换为element

268 elementData[index] =element;269 returnoldValue;270 }271

272 /**

273 * 返回在list列表中的指定元素第一次出现的索引值274 * Returns the index of the first occurrence of the specified element275 * in this list, or -1 if this list does not contain the element.276 * More formally, returns the lowest index i such that277 * (o==null ? get(i)==null : o.equals(get(i))),278 * or -1 if there is no such index.279 */

280 public intindexOf(Object o) {281 if (o == null) {282 for (int i = 0; i < size; i++)283 if (elementData[i]==null)284 returni;285 } else{286 for (int i = 0; i < size; i++)287 if(o.equals(elementData[i]))288 returni;289 }290 return -1;291 }292 //从头开始查找元素,可以查找null值

293

294 /**

295 * 返回在list列表中指定位置的元素296 * Returns the element at the specified position in this list.297 *298 *@paramindex index of the element to return299 *@returnthe element at the specified position in this list300 *@throwsIndexOutOfBoundsException {@inheritDoc}301 */

302 public E get(intindex) {303 //范围检查,只检查>=size的部分

304 rangeCheck(index);305

306 returnelementData(index);307 }308

309 E elementData(intindex) {310 return(E) elementData[index];311 }312 //注意:返回的值都做了向下转型(object->E)处理

313 }

java arraylist范围_Java常见集合之ArrayList深入分析相关推荐

  1. java基础:13.1 集合框架 - ArrayList

    文章目录 1.ArrayList 2.特点 3.和数组的区别 4.常用的方法 5.使用泛型与不使用泛型 6.遍历 7.练习 1.ArrayList 在 java基础:6.0 ArrayList 中,初 ...

  2. java list 区别_Java中List和ArrayList的区别

    List是一个接口,而ListArray是一个类. ListArray继承并实现了List. 所以List不能被构造,但可以向上面那样为List创建一个引用,而ListArray就可以被构造. Lis ...

  3. java 有序容器_Java 容器集合框架概览

    Java Collections Framework 集合的概念 集合collection,有时叫做容器container,把多个元素组成一个单元. 早期的Java (pre-1.2) 中包含了Vec ...

  4. java基础习题集_java基础集合经典训练题

    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...

  5. java 自定义运算符_Java中集合的自定义运算符

    java 自定义运算符 总览 操作员重载有多种语言可用. Java对String类型的+运算符的支持对运算符的重载非常有限. 我们可以利用其他语言支持运算符的不同方式,但是我们可以在Java中实现一个 ...

  6. java 最接近_Java在集合中查找最接近(或相等)的值

    我有一个类: public class Observation { private String time; private double x; private double y; //Constru ...

  7. java java算法题目_java常见算法题目

    1: JAVA经典算法40题 2: [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 3 ...

  8. java百度文库_java 常见异常 (百度文库)

    1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...

  9. java关键字吗_JAVA常见关键字

    JAVA的关键字都是小写的 在JAVA中目前一共有53个关键字:其中由51+2个保留字=53个关键字 1.JAVA的保留关键字(2个) const--常量,常数:用于修改字段或局部变量的声明. got ...

最新文章

  1. Intellij-Cannot download Sources解决方法
  2. easyui mysql手册_easyui api 中文
  3. HDU 1027 G - Can you answer these queries?
  4. 计算机考试模拟系统无法进入,全国计算机等级考试上机考试模拟系统使用说明...
  5. 《程序设计技术》第六章例程
  6. python Udp与Tcp
  7. spark 部署方式
  8. U盘的针脚板竟然掉了
  9. 苹果cms10整合dplayer播放器
  10. reactos操作系统实现(109)
  11. 遗传算法框架deap简介与使用
  12. 字节跳动面试题 问题定位
  13. 运维工程师种种尴尬的瞬间情景,你有无?
  14. Julia之初体验(九)字符串连接与匹配
  15. PMP备考大全:经典题库(7月第4周)
  16. 面试常问的 C/C++ 问题,你能答上来几个?
  17. GoLang音视频转码
  18. 股票投资 策略(收集)
  19. fpu测试_正点原子STM32F4/F7水星开发板资料连载第五十章 FPU 测试实验
  20. 计算机硬件 系统安装维护教程 04系统安装-Win-03:OOBE设置

热门文章

  1. swagger接口文档使用
  2. dataGridview与下拉框高级绑定
  3. 1017 A除以B (20分)
  4. leetcode 112路径总和
  5. 【C语言进阶深度学习记录】十二 C语言中的:字符和字符串
  6. 【C++深度剖析教程17】逻辑操作符的陷阱
  7. Linux空硬盘从分区到挂载
  8. js实现table合并相同列单元格
  9. css用hover制作下拉菜单
  10. KVM--安装及初步使用