• ArrayList源码


    1. package java.util;
    2. import java.util.function.Consumer;
    3. import java.util.function.Predicate;
    4. import java.util.function.UnaryOperator;
    5. /**
    6. * 可以改变大小的List的实现,实现所有的List的操作,允许保存各种元素,包括null。除了实现
    7. * List接口之外,这个类提供了操作内部存储数据的数组大小的方法。(这个类相当于Vector,
    8. * 但是它并不是线程同步的,不是线程安全的)
    9. *
    10. * size,isEmpty,set,iterator,listIterator方法运行时间为常量时间,add方法运行为摊还常量
    11. * 时间,也即增加n个元素的时间为O(n)。其他的操作运行时间大致为线性时间,常数因子相较
    12. * 于LinkedList更小
    13. *
    14. * 每个ArrayList实例都有一个容量,是存储数据的数组的大小,他至少是List的元素数量的大小。
    15. * 随着元素的增加,容量自动增加。容量增大的细节在添加一个元素有恒定的时间成本摊销的基础上
    16. *
    17. * 在添加大量元素前,应用程序可以使用ensureCapacity方法增大Arraylist的容量, 这可能减少
    18. * 增量重新分配的次数
    19. *
    20. * 这个实现是非线程安全的,如果有多个线程同时操作一个ArrayList实例,其中至少有一个线程
    21. * 在修改这个实例的结构,则必须在外部使用同步控制(一个结构上的修改操作指的是添加或删除一个
    22. * 或多个元素,或者明确改变存储数据的数组大小,仅仅改变元素值不是结构上的修改),这一般由
    23. * 封装ArrayList的类来进行。
    24. *
    25. * 如果没有这样的对象存在,列表应该使用Collections.synchronizedList方法来获得同步的
    26. * (线程安全的)装饰对象(代理对象)。这个操作最好在创建的时候进行,来防止意外的非同步的
    27. * 操作。还可以使用concurrent包下的CopyOnWriteArrayList代替ArrayList的使用
    28. *
    29. *
    30. * 使用类的iterator()和listIterator(int)方法会出发fail-fast错误机制(抛出
    31. * ConcurrentModificationException异常),如果创建了iterator后进行结构上的修改,除了使
    32. * 用iterator的remove或者add方法。因此,在并发修改时,iterator快速失败并且清除,而不是冒
    33. * 着将来可能发生的不确定的风险。
    34. *
    35. * 迭代器的fail-fast错误机制不能被保证,通常来说,很难保证在非同步并发修改操作的fail-fast
    36. * 机制。fail-fast错误机制的迭代器力所能及的抛出ConcurrentModificationException。
    37. * 所以,开发时不应该依赖这个异常保证程序的正确性,仅仅在发现bug时使用
    38. * 继承了AbstractList,可以继承部分默认的实现
    39. * 实现了Cloneable, java.io.Serializable,允许克隆和序列化
    40. */
    41. publicclassArrayList<E>extendsAbstractList<E>
    42. implementsList<E>,RandomAccess,Cloneable, java.io.Serializable
    43. {
    44. //序列号
    45. privatestaticfinallong serialVersionUID =8683452581122892189L;
    46. /**
    47. * 列表默认容量
    48. */
    49. privatestaticfinalint DEFAULT_CAPACITY =10;
    50. /**
    51. * 共享空数组实例,用于空实例。调用构造函数容量为0时,会赋予数据的数组
    52. */
    53. privatestaticfinalObject[] EMPTY_ELEMENTDATA ={};
    54. /**
    55. * 共享空数组实例用于默认大小的空实例。使用默认构造函数时,会赋予数据的数组
    56. */
    57. privatestaticfinalObject[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA ={};
    58. /**
    59. * 存储数据的数组,数组大小就是列表的容量的大小。在第一次加入元素时,任何空实例
    60. * (DEFAULTCAPACITY_EMPTY_ELEMENTDATA)会扩充成默认大小。
    61. * transient表示序列化过程不希望被序列化的字段,ArrayList自己实现的序列化机制会
    62. * 逐个序列化每个元素
    63. */
    64. transientObject[] elementData;// non-private to simplify nested class access
    65. /**
    66. * 列表中元素的数量
    67. *
    68. * @serial
    69. */
    70. privateint size;
    71. /**
    72. * 构造一个指定容量的空列表
    73. *
    74. * @param initialCapacity 列表的初始容量
    75. * @throws IllegalArgumentException 传入的初始容量为负数时,抛出次异常
    76. */
    77. publicArrayList(int initialCapacity){
    78. if(initialCapacity >0){
    79. this.elementData =newObject[initialCapacity];
    80. }elseif(initialCapacity ==0){
    81. this.elementData = EMPTY_ELEMENTDATA;
    82. }else{
    83. thrownewIllegalArgumentException("Illegal Capacity: "+
    84. initialCapacity);
    85. }
    86. }
    87. /**
    88. * 构造一个默认大小的空列表
    89. */
    90. publicArrayList(){
    91. this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    92. }
    93. /**
    94. * 根据传入的集合,构造一个列表,元素的顺序是传入集合的迭代器返回的顺序
    95. *
    96. * @param c 传入的容器
    97. * @throws NullPointerException 传入的容器为空时抛出异常
    98. */
    99. publicArrayList(Collection<?extends E> c){
    100. elementData = c.toArray();
    101. if((size = elementData.length)!=0){
    102. // c.toArray()可能没有返回 Object[]
    103. if(elementData.getClass()!=Object[].class)
    104. //转成Object类型
    105. elementData =Arrays.copyOf(elementData, size,Object[].class);
    106. }else{
    107. // 如果传入的集合没有元素,则使用空数组代替
    108. this.elementData = EMPTY_ELEMENTDATA;
    109. }
    110. }
    111. /**
    112. * 减小列表的容器大小至列表中元素数量
    113. */
    114. publicvoid trimToSize(){
    115. modCount++;
    116. if(size < elementData.length){
    117. elementData =(size ==0)
    118. ? EMPTY_ELEMENTDATA
    119. :Arrays.copyOf(elementData, size);
    120. }
    121. }
    122. /**
    123. * 增大ArrayList实例的容量, 如果必要,保证能够存储传入的最小容量个元素
    124. *
    125. * @param minCapacity 需要的最少的存储容量
    126. */
    127. publicvoid ensureCapacity(int minCapacity){
    128. int minExpand =(elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
    129. // any size if not default element table
    130. ?0
    131. // larger than default for default empty table. It's already
    132. // supposed to be at default size.
    133. : DEFAULT_CAPACITY;
    134. if(minCapacity > minExpand){
    135. ensureExplicitCapacity(minCapacity);
    136. }
    137. }
    138. privatevoid ensureCapacityInternal(int minCapacity){
    139. //默认大小数组,将数组扩展成默认或者minCapacity的大的容量
    140. if(elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
    141. minCapacity =Math.max(DEFAULT_CAPACITY, minCapacity);
    142. }
    143. ensureExplicitCapacity(minCapacity);
    144. }
    145. //确保数组能够保存足够的元素
    146. privatevoid ensureExplicitCapacity(int minCapacity){
    147. modCount++;
    148. // 当要求的数量大于数组的大小,扩充成需要的数量
    149. if(minCapacity - elementData.length >0)
    150. grow(minCapacity);
    151. }
    152. /**
    153. * 最大允许的容量
    154. * 一些虚拟机会存储一些头信息
    155. * 尝试分配大容量的数组会导致OutOfMemoryError,超出虚拟机的限制
    156. */
    157. privatestaticfinalint MAX_ARRAY_SIZE =Integer.MAX_VALUE -8;
    158. /**
    159. * 扩充容量以保证能够存储需要的容量(传入的参数)
    160. *
    161. * @param minCapacity 需要的容量大小
    162. */
    163. privatevoid grow(int minCapacity){
    164. // overflow-conscious code
    165. int oldCapacity = elementData.length;
    166. //扩充原来容量的1/2,即扩大1.5倍
    167. int newCapacity = oldCapacity +(oldCapacity >>1);
    168. //如果需要的容量还是不够,则扩充至需要的容量的大小
    169. if(newCapacity - minCapacity <0)
    170. newCapacity = minCapacity;
    171. /**
    172. * 如果超出定义的最大容量,扩充至定义的最大容量(需要的容量小于等于定义的最大
    173. * 容量)或最大int的值(需要的容量大小大于定义的最大容量)
    174. * */
    175. if(newCapacity - MAX_ARRAY_SIZE >0)
    176. newCapacity = hugeCapacity(minCapacity);
    177. // minCapacity is usually close to size, so this is a win:
    178. elementData =Arrays.copyOf(elementData, newCapacity);
    179. }
    180. // 根据需要的容量大小,获得超大容量
    181. privatestaticint hugeCapacity(int minCapacity){
    182. if(minCapacity <0)// 如果minCapacity小于0,则超出最大整数值
    183. thrownewOutOfMemoryError();
    184. return(minCapacity > MAX_ARRAY_SIZE)?
    185. Integer.MAX_VALUE :
    186. MAX_ARRAY_SIZE;
    187. }
    188. /**
    189. * 返回列表的中元素的数量
    190. *
    191. * @return 列表的中元素的数量
    192. */
    193. publicint size(){
    194. return size;
    195. }
    196. /**
    197. * 判断列表是否为空
    198. *
    199. * @return 当列表中没有元素则返回true,否则返回false
    200. */
    201. publicboolean isEmpty(){
    202. return size ==0;
    203. }
    204. /**
    205. * 当列表中存在传入的元素,返回true。进一步,列表中至少有一个传入的元素,则返回true
    206. * 比如 o为null,则判断列表中是否有null元素,返回o与列表中的元素e的equals()比较的结果
    207. *
    208. * @param 需要检验是否在列表中的元素的对象
    209. * @return 如果列表中存在o则返回true
    210. */
    211. publicboolean contains(Object o){
    212. return indexOf(o)>=0;
    213. }
    214. /**
    215. * 返回列表中第一次出现的o(equals方法返回true)的索引,如果不存在则返回-1
    216. */
    217. publicint indexOf(Object o){
    218. if(o ==null){
    219. for(int i =0; i < size; i++)
    220. if(elementData[i]==null)
    221. return i;
    222. }else{
    223. for(int i =0; i < size; i++)
    224. if(o.equals(elementData[i]))
    225. return i;
    226. }
    227. return-1;
    228. }
    229. /**
    230. * 返回列表中最后一次出现的o(equals方法返回true)的索引,如果不存在则返回-1
    231. */
    232. publicint lastIndexOf(Object o){
    233. if(o ==null){
    234. for(int i = size-1; i >=0; i--)
    235. if(elementData[i]==null)
    236. return i;
    237. }else{
    238. for(int i = size-1; i >=0; i--)
    239. if(o.equals(elementData[i]))
    240. return i;
    241. }
    242. return-1;
    243. }
    244. /**
    245. * 返回ArrayList实例的浅表复制,不复制列表的元素(都指向相同的对象,没有创建新的对
    246. * 象)。
    247. */
    248. publicObject clone(){
    249. try{
    250. ArrayList<?> v =(ArrayList<?>)super.clone();
    251. v.elementData =Arrays.copyOf(elementData, size);
    252. v.modCount =0;
    253. return v;
    254. }catch(CloneNotSupportedException e){
    255. // this shouldn't happen, since we are Cloneable
    256. thrownewInternalError(e);
    257. }
    258. }
    259. /**
    260. * 获得按照列表顺序存储的数组,这个方法需要分配内存存储拷贝的数组。(浅复制)
    261. *
    262. * @return an array containing all of the elements in this list in
    263. * proper sequence
    264. */
    265. publicObject[] toArray(){
    266. returnArrays.copyOf(elementData, size);
    267. }
    268. /**
    269. * 获得按照列表顺序存储的数组,返回一个泛型数组,这个方法需要分配内存存储拷贝的数组
    270. * (浅复制),返回的数组的类型应该是参数指定的类型。如果传入的数组大小大于列表的元素
    271. * 则返回,传入的数组,否则创建一个新的数组,并返回新创建的数组
    272. *
    273. * 如果传入的数组长度大于列表的长度,则设置其第size个元素为null(当且仅当在知道list中
    274. * 没有null元素时,用于判断list中元素个数)
    275. * @param a 如果足够大,存储列表中的元素,否则根据传入的运行时参数创建一个新的数组
    276. * @return 返回列表元素的一个拷贝
    277. * @throws ArrayStoreException if the runtime type of the specified array
    278. * is not a supertype of the runtime type of every element in
    279. * this list
    280. * @throws NullPointerException if the specified array is null
    281. */
    282. @SuppressWarnings("unchecked")
    283. public<T> T[] toArray(T[] a){
    284. if(a.length < size)
    285. // Make a new array of a's runtime type, but my contents:
    286. return(T[])Arrays.copyOf(elementData, size, a.getClass());
    287. System.arraycopy(elementData,0, a,0, size);
    288. if(a.length > size)
    289. a[size]=null;
    290. return a;
    291. }
    292. /**
    293. * 根据索引,获得列表中第index个元素,首先检查下标是否在正确的范围
    294. */
    295. @SuppressWarnings("unchecked")
    296. E elementData(int index){
    297. return(E) elementData[index];
    298. }
    299. /**
    300. * 返回第index个元素
    301. *
    302. * @param index index of the element to return
    303. * @return the element at the specified position in this list
    304. * @throws IndexOutOfBoundsException {@inheritDoc}
    305. */
    306. public E get(int index){
    307. rangeCheck(index);
    308. return elementData(index);
    309. }
    310. /**
    311. * 将第index个元素的值修改为传入的元素值,首先检查下标是否在正确的范围,返回修改前的值
    312. *
    313. * @param index 元素的索引
    314. * @param element 设置到index的值
    315. * @return 原来位于index的值
    316. * @throws IndexOutOfBoundsException {@inheritDoc}
    317. */
    318. public E set(int index, E element){
    319. rangeCheck(index);
    320. E oldValue = elementData(index);
    321. elementData[index]= element;
    322. return oldValue;
    323. }
    324. /**
    325. * 在列表的末尾添加元素,修改列表的元素数量(size)。首先需要确保列表的容量足够存放新
    326. * 添加的元素。
    327. *
    328. * @param e element to be appended to this list
    329. * @return <tt>true</tt> (as specified by {@link Collection#add})
    330. */
    331. publicboolean add(E e){
    332. ensureCapacityInternal(size +1);// 确保能够存放添加元素,容量不足则扩容
    333. elementData[size++]= e;
    334. returntrue;
    335. }
    336. /**
    337. * 在列表第index个位置插入一个元素,index及其后面的元素向后移动一个位置,然后将需要插
    338. * 入的值存入第index的位置
    339. *
    340. * @param index index at which the specified element is to be inserted
    341. * @param element element to be inserted
    342. * @throws IndexOutOfBoundsException {@inheritDoc}
    343. */
    344. publicvoid add(int index, E element){
    345. rangeCheckForAdd(index);
    346. ensureCapacityInternal(size +1);// 确保有足够的存储空间
    347. System.arraycopy(elementData, index, elementData, index +1,
    348. size - index);
    349. elementData[index]= element;
    350. size++;
    351. }
    352. /**
    353. * 移除列表中第index个元素,index后面的元素向前移动一个位置,列表元素数量减小1
    354. *
    355. * @param index 移除的元素的下标
    356. * @return 返回被删除的元素
    357. * @throws IndexOutOfBoundsException {@inheritDoc}
    358. */
    359. public E remove(int index){
    360. rangeCheck(index);
    361. modCount++;//
    362. E oldValue = elementData(index);
    363. int numMoved = size - index -1;
    364. if(numMoved >0)
    365. System.arraycopy(elementData, index+1, elementData, index,
    366. numMoved);
    367. elementData[--size]=null;// 释放,以让垃圾回收器回收
    368. return oldValue;
    369. }
    370. /**
    371. * 移除列表中第一个o(o为null时,第一个null元素,否则,是第一个equals()为true的元
    372. * 素),如果过不存在,则不改变。如果列表中存在这样的元素,则返回true,否则返回false
    373. *
    374. * @param o element to be removed from this list, if present
    375. * @return <tt>true</tt> if this list contained the specified element
    376. */
    377. publicboolean remove(Object o){
    378. if(o ==null){
    379. for(int index =0; index < size; index++)
    380. if(elementData[index]==null){
    381. fastRemove(index);
    382. returntrue;
    383. }
    384. }else{
    385. for(int index =0; index < size; index++)
    386. if(o.equals(elementData[index])){
    387. fastRemove(index);
    388. returntrue;
    389. }
    390. }
    391. returnfalse;
    392. }
    393. /*
    394. * 快速移除元素,不检查索引范围,不返回被删除元素
    395. */
    396. privatevoid fastRemove(int index){
    397. modCount++;
    398. int numMoved = size - index -1;
    399. if(numMoved >0)
    400. System.arraycopy(elementData, index+1, elementData, index,
    401. numMoved);
    402. elementData[--size]=null;// 释放,以让垃圾回收器回收
    403. }
    404. /**
    405. * 移除列表中所有的元素,操作后,列表为空
    406. */
    407. publicvoid clear(){
    408. modCount++;
    409. // 释放所有引用,以让垃圾收集器回收
    410. for(int i =0; i < size; i++)
    411. elementData[i]=null;
    412. //列表大小改为0
    413. size =0;
    414. }
    415. /**
    416. * 向列表末尾添加c中的所有元素,顺序是c.iterator()返回的顺序,添加了元素返回true。
    417. *
    418. * @param c collection containing elements to be added to this list
    419. * @return <tt>true</tt> if this list changed as a result of the call
    420. * @throws NullPointerException if the specified collection is null
    421. */
    422. publicboolean addAll(Collection<?extends E> c){
    423. Object[] a = c.toArray();
    424. int numNew = a.length;
    425. ensureCapacityInternal(size + numNew);// Increments modCount
    426. System.arraycopy(a,0, elementData, size, numNew);
    427. size += numNew;
    428. return numNew !=0;
    429. }
    430. /**
    431. * 向列表第index位置起添加c中的所有元素,顺序是c.iterator()返回的顺序,index及其后面
    432. * 的元素向后移动c中元素个位置,添加了元素返回true。首先需要检查index是否是在范围内
    433. *
    434. * @param index index at which to insert the first element from the
    435. * specified collection
    436. * @param c collection containing elements to be added to this list
    437. * @return <tt>true</tt> if this list changed as a result of the call
    438. * @throws IndexOutOfBoundsException {@inheritDoc}
    439. * @throws NullPointerException if the specified collection is null
    440. */
    441. publicboolean addAll(int index,Collection<?extends E> c){
    442. rangeCheckForAdd(index);
    443. Object[] a = c.toArray();
    444. int numNew = a.length;
    445. ensureCapacityInternal(size + numNew);// Increments modCount
    446. int numMoved = size - index;
    447. if(numMoved >0)
    448. System.arraycopy(elementData, index, elementData, index + numNew,
    449. numMoved);
    450. System.arraycopy(a,0, elementData, index, numNew);
    451. size += numNew;
    452. return numNew !=0;
    453. }
    454. /**
    455. * 移除元素,移除[fromIndex,toIndex)范围的元素,不包括toIndex
    456. *
    457. * @throws IndexOutOfBoundsException if {@code fromIndex} or
    458. * {@code toIndex} is out of range
    459. * ({@code fromIndex < 0 ||
    460. * fromIndex >= size() ||
    461. * toIndex > size() ||
    462. * toIndex < fromIndex})
    463. */
    464. protectedvoid removeRange(int fromIndex,int toIndex){
    465. modCount++;
    466. int numMoved = size - toIndex;
    467. System.arraycopy(elementData, toIndex, elementData, fromIndex,
    468. numMoved);
    469. // 释放(移动后列表最后一个元素之后的)引用,以让垃圾收集器回收
    470. int newSize = size -(toIndex-fromIndex);
    471. for(int i = newSize; i < size; i++){
    472. elementData[i]=null;
    473. }
    474. size = newSize;
    475. }
    476. /**
    477. * 检查索引值是否在合理的范围内,不在则抛出异常
    478. * negative: It is always used immediately prior to an array access,
    479. * which throws an ArrayIndexOutOfBoundsException if index is negative.
    480. */
    481. privatevoid rangeCheck(int index){
    482. if(index >= size)
    483. thrownewIndexOutOfBoundsException(outOfBoundsMsg(index));
    484. }
    485. /**
    486. * 检查索引值是否在合理的范围内,不在则抛出异常
    487. */
    488. privatevoid rangeCheckForAdd(int index){
    489. if(index > size || index <0)
    490. thrownewIndexOutOfBoundsException(outOfBoundsMsg(index));
    491. }
    492. /**
    493. * 生成数组越界异常信息
    494. */
    495. privateString outOfBoundsMsg(int index){
    496. return"Index: "+index+", Size: "+size;
    497. }
    498. /**
    499. * 移除列表中的c中包含的所有元素
    500. *
    501. * @param c 需要移除的元素集合,不能为空
    502. * @return 如果列表改变了,则返回true,否则返回false
    503. * @throws ClassCastException 如果传入的集合中有与列表中元素类型不匹配时,抛出该异
    504. * 常
    505. * @throws NullPointerException ArrayList不支持null,但c中包含null元素时,抛出该
    506. * 异常
    507. */
    508. publicboolean removeAll(Collection<?> c){
    509. Objects.requireNonNull(c);
    510. return batchRemove(c,false);
    511. }
    512. /**
    513. * 保留列表中与c中共有的元素(求交集)。
    514. *
    515. * @param c 需要完成交集的集合,不允许为空
    516. * @return 如果列表改变了,则返回true,否则返回false
    517. * @throws ClassCastException 如果传入的集合中有与列表中元素类型不匹配时,抛出该异
    518. * 常
    519. * @throws NullPointerException ArrayList不支持null,但c中包含null元素时,抛出该
    520. * 异常
    521. */
    522. publicboolean retainAll(Collection<?> c){
    523. Objects.requireNonNull(c);
    524. return batchRemove(c,true);
    525. }
    526. /**
    527. * 批量删除, complement为true时,保留与c中相同的元素,否则,保留与c中不相同的元素
    528. */
    529. privateboolean batchRemove(Collection<?> c,boolean complement){
    530. finalObject[] elementData =this.elementData;
    531. int r =0, w =0;
    532. boolean modified =false;
    533. try{
    534. for(; r < size; r++)
    535. if(c.contains(elementData[r])== complement)
    536. elementData[w++]= elementData[r];
    537. }finally{
    538. //保护动作,当c.contains()抛出异常时,将剩余的(r之后的)元素保留
    539. if(r != size){
    540. System.arraycopy(elementData, r,
    541. elementData, w,
    542. size - r);
    543. w += size - r;
    544. }
    545. if(w != size){
    546. // 有元素需要被移除时,释放引用,以让垃圾回收器回收
    547. for(int i = w; i < size; i++)
    548. elementData[i]=null;
    549. modCount += size - w;
    550. size = w;
    551. modified =true;
    552. }
    553. }
    554. return modified;
    555. }
    556. /**
    557. * 将一个ArrayList实例的状态保存到流中,也即序列化,这是类实现的序列化机制,不是默认
    558. * 的序列化机制
    559. *
    560. * @serialData The length of the array backing the <tt>ArrayList</tt>
    561. * instance is emitted (int), followed by all of its elements
    562. * (each an <tt>Object</tt>) in the proper order.
    563. */
    564. privatevoid writeObject(java.io.ObjectOutputStream s)
    565. throws java.io.IOException{
    566. // Write out element count, and any hidden stuff
    567. int expectedModCount = modCount;
    568. s.defaultWriteObject();//默认序列化,记录列表中元素的个数
    569. // 将列表大小作为容量保存,以此适用于clone()
    570. s.writeInt(size);
    571. // 按照正确顺序保存列表中的元素
    572. for(int i=0; i<size; i++){
    573. s.writeObject(elementData[i]);
    574. }
    575. //序列化过程中是否有修改列表的结构(扩容,添加,移除操作)
    576. //如果有,则抛出ConcurrentModificationException异常
    577. if(modCount != expectedModCount){
    578. thrownewConcurrentModificationException();
    579. }
    580. }
    581. /**
    582. * 从流中读取并重新创建Arraylist,也即反序列化
    583. */
    584. privatevoid readObject(java.io.ObjectInputStream s)
    585. throws java.io.IOException,ClassNotFoundException{
    586. elementData = EMPTY_ELEMENTDATA;
    587. // Read in size, and any hidden stuff
    588. s.defaultReadObject();//默认反序列化,读取列表元素个数
    589. // 读取容量,忽略
    590. s.readInt();// ignored
    591. if(size >0){
    592. // 类似于clone(),创建大小与元素个数相同的数组
    593. ensureCapacityInternal(size);
    594. Object[] a = elementData;
    595. // 以正确的顺序读取所有的元素
    596. for(int i=0; i<size; i++){
    597. a[i]= s.readObject();
    598. }
    599. }
    600. }
    601. /**
    602. * 创建一个从index开始的ListIterator(第一次调用next,返回index处的元素),调用
    603. * previous,则返回index-1处的元素,首先检查index的正确性
    604. *
    605. * The returned list iterator is fail-fast
    606. *
    607. * @throws IndexOutOfBoundsException {@inheritDoc}
    608. */
    609. publicListIterator<E> listIterator(int index){
    610. if(index <0|| index > size)
    611. thrownewIndexOutOfBoundsException("Index: "+index);
    612. returnnewListItr(index);
    613. }
    614. /**
    615. * 重载,创建一个默认从0开始的ListIterator
    616. *
    617. * The returned list iterator is fail-fast
    618. *
    619. * @see #listIterator(int)
    620. */
    621. publicListIterator<E> listIterator(){
    622. returnnewListItr(0);
    623. }
    624. /**
    625. * 按照正确的顺序,产生一个iterator
    626. *
    627. * @return an iterator over the elements in this list in proper sequence
    628. */
    629. publicIterator<E> iterator(){
    630. returnnewItr();
    631. }
    632. /**
    633. * AbstractList.Itr的优化
    634. */
    635. privateclassItrimplementsIterator<E>{
    636. int cursor;// 下一个元素的游标(索引)
    637. int lastRet =-1;// 上一次返回的元素的索引
    638. int expectedModCount = modCount;
    639. //是否存在下一个,没有遍历到末尾,返回true
    640. publicboolean hasNext(){
    641. return cursor != size;
    642. }
    643. //返回下一个元素
    644. @SuppressWarnings("unchecked")
    645. public E next(){
    646. //检查迭代器过程中,是否有修改ArrayList的实例的结构
    647. //有则抛出ConcurrentModificationException异常
    648. checkForComodification();
    649. int i = cursor;
    650. if(i >= size)
    651. thrownewNoSuchElementException();//超出size范围,则抛出异常
    652. Object[] elementData =ArrayList.this.elementData;
    653. if(i >= elementData.length)
    654. thrownewConcurrentModificationException();
    655. cursor = i +1;//记录下一个元素的游标值
    656. return(E) elementData[lastRet = i];
    657. }
    658. //移除当前的元素(next()获得的元素,cursor已经指向下一个)
    659. publicvoid remove(){
    660. //上一个元素不存在
    661. if(lastRet <0)
    662. thrownewIllegalStateException();
    663. checkForComodification();//检查是否修改
    664. try{
    665. ArrayList.this.remove(lastRet);//移除lastRet处的元素
    666. cursor = lastRet;
    667. lastRet =-1;
    668. expectedModCount = modCount;
    669. }catch(IndexOutOfBoundsException ex){
    670. thrownewConcurrentModificationException();
    671. }
    672. }
    673. /**
    674. * 对剩余未迭代的元素进行指定操作,直到所有的元素都已经被处理或行动将抛出一个异常
    675. */
    676. @Override
    677. @SuppressWarnings("unchecked")
    678. publicvoid forEachRemaining(Consumer<?super E> consumer){
    679. Objects.requireNonNull(consumer);
    680. finalint size =ArrayList.this.size;
    681. int i = cursor;
    682. //已经遍历完成,不需要继续遍历
    683. if(i >= size){
    684. return;
    685. }
    686. finalObject[] elementData =ArrayList.this.elementData;
    687. if(i >= elementData.length){
    688. thrownewConcurrentModificationException();
    689. }
    690. while(i != size && modCount == expectedModCount){
    691. consumer.accept((E) elementData[i++]);
    692. }
    693. // 更新游标和上一次返回的元素,并检测是否修改了列表结构
    694. cursor = i;
    695. lastRet = i -1;
    696. checkForComodification();
    697. }
    698. finalvoid checkForComodification(){
    699. if(modCount != expectedModCount)
    700. thrownewConcurrentModificationException();
    701. }
    702. }
    703. /**
    704. * AbstractList.ListItr的优化,继承自Iter
    705. */
    706. privateclassListItrextendsItrimplementsListIterator<E>{
    707. ListItr(int index){
    708. super();
    709. cursor = index;//指定第一个需要返回的元素为index
    710. }
    711. //返回是否有前一个元素,直到到列表的第0个元素
    712. publicboolean hasPrevious(){
    713. return cursor !=0;
    714. }
    715. //返回下一个元素的游标
    716. publicint nextIndex(){
    717. return cursor;
    718. }
    719. //返回上一个元素的游标
    720. publicint previousIndex(){
    721. return cursor -1;
    722. }
    723. //获取上一个元素
    724. @SuppressWarnings("unchecked")
    725. public E previous(){
    726. checkForComodification();//检查是否修改了列表的结构
    727. int i = cursor -1;//获得上一个元素索引
    728. //数组越界则抛出异常
    729. if(i <0)
    730. thrownewNoSuchElementException();
    731. Object[] elementData =ArrayList.this.elementData;
    732. if(i >= elementData.length)
    733. thrownewConcurrentModificationException();
    734. cursor = i;//修改游标,下一个元素还是当前返回的previous元素
    735. return(E) elementData[lastRet = i];
    736. }
    737. //设置当前元素(lastRet指向的值)的值为e
    738. publicvoidset(E e){
    739. if(lastRet <0)
    740. thrownewIllegalStateException();
    741. checkForComodification();
    742. try{
    743. ArrayList.this.set(lastRet, e);
    744. }catch(IndexOutOfBoundsException ex){
    745. thrownewConcurrentModificationException();
    746. }
    747. }
    748. //在下一个游标处添加一个元素(remove的时候,游标回到移除的元素位置)
    749. publicvoid add(E e){
    750. checkForComodification();
    751. try{
    752. int i = cursor;
    753. ArrayList.this.add(i, e);
    754. cursor = i +1;
    755. lastRet =-1;
    756. expectedModCount = modCount;
    757. }catch(IndexOutOfBoundsException ex){
    758. thrownewConcurrentModificationException();
    759. }
    760. }
    761. }
    762. /**
    763. * 返回一个子列表,子列表的范围为列表[fromIndex,toIndex)之间的元素,不包括同Index
    764. * 如果fromIndex和同Index相同,则返回空列表
    765. * 不创建新的List,在SubList中持有一个当前ArrayList实例的引用,以及子列表的索引范围
    766. * 子列表的语义将变得不明确如果当前ArrayList实例结构发生变化(增删、扩容)
    767. * @throws IndexOutOfBoundsException {@inheritDoc}
    768. * @throws IllegalArgumentException {@inheritDoc}
    769. */
    770. publicList<E> subList(int fromIndex,int toIndex){
    771. subListRangeCheck(fromIndex, toIndex, size);
    772. returnnewSubList(this,0, fromIndex, toIndex);
    773. }
    774. //检查子列表范围是否正确
    775. staticvoid subListRangeCheck(int fromIndex,int toIndex,int size){
    776. if(fromIndex <0)
    777. thrownewIndexOutOfBoundsException("fromIndex = "+ fromIndex);
    778. if(toIndex > size)
    779. thrownewIndexOutOfBoundsException("toIndex = "+ toIndex);
    780. if(fromIndex > toIndex)
    781. thrownewIllegalArgumentException("fromIndex("+ fromIndex +
    782. ") > toIndex("+ toIndex +")");
    783. }
    784. /*Sublist-Start*/
    785. //定义子列表类,维持一个获得子列表的ArrayList实例以及偏移量
    786. //子列表偏移量和列表大小
    787. privateclassSubListextendsAbstractList<E>implementsRandomAccess{
    788. privatefinalAbstractList<E> parent;
    789. privatefinalint parentOffset;
    790. privatefinalint offset;
    791. int size;
    792. //构造方法,传入父列表,子列表偏移量,开始的索引和结束的索引
    793. SubList(AbstractList<E> parent,
    794. int offset,int fromIndex,int toIndex){
    795. this.parent = parent;
    796. this.parentOffset = fromIndex;
    797. this.offset = offset + fromIndex;
    798. this.size = toIndex - fromIndex;
    799. this.modCount =ArrayList.this.modCount;
    800. }
    801. //设置index处的值为e,直接操作父列表,需要检查范围
    802. public E set(int index, E e){
    803. rangeCheck(index);
    804. checkForComodification();
    805. E oldValue =ArrayList.this.elementData(offset + index);
    806. ArrayList.this.elementData[offset + index]= e;
    807. return oldValue;
    808. }
    809. //获取index处的值,需要检查范围,并检查是否有修改
    810. public E get(int index){
    811. rangeCheck(index);
    812. checkForComodification();
    813. returnArrayList.this.elementData(offset + index);
    814. }
    815. //获得子列表的大小,检查是否有修改
    816. publicint size(){
    817. checkForComodification();
    818. returnthis.size;
    819. }
    820. //在index处添加一个元素e,需要检查范围和是否有修改
    821. publicvoid add(int index, E e){
    822. rangeCheckForAdd(index);
    823. checkForComodification();
    824. parent.add(parentOffset + index, e);
    825. this.modCount = parent.modCount;
    826. this.size++;
    827. }
    828. //移除index处的元素,并返回删除的元素
    829. public E remove(int index){
    830. rangeCheck(index);
    831. checkForComodification();
    832. E result = parent.remove(parentOffset + index);
    833. this.modCount = parent.modCount;
    834. this.size--;
    835. return result;
    836. }
    837. //移除[fromIndex,toIndex)之间的元素,检查是否有修改列表结构
    838. protectedvoid removeRange(int fromIndex,int toIndex){
    839. checkForComodification();
    840. parent.removeRange(parentOffset + fromIndex,
    841. parentOffset + toIndex);
    842. this.modCount = parent.modCount;
    843. this.size -= toIndex - fromIndex;
    844. }
    845. //在子列表末尾添加c中所有元素
    846. publicboolean addAll(Collection<?extends E> c){
    847. return addAll(this.size, c);
    848. }
    849. //在子列表index起添加c中所有元素,需要检查范围和是否有修改
    850. publicboolean addAll(int index,Collection<?extends E> c){
    851. rangeCheckForAdd(index);
    852. int cSize = c.size();
    853. if(cSize==0)
    854. returnfalse;
    855. checkForComodification();
    856. parent.addAll(parentOffset + index, c);
    857. this.modCount = parent.modCount;
    858. this.size += cSize;
    859. returntrue;
    860. }
    861. //获得迭代器
    862. publicIterator<E> iterator(){
    863. return listIterator();
    864. }
    865. //获得ListIterator迭代器(可向前、向后)
    866. publicListIterator<E> listIterator(finalint index){
    867. checkForComodification();
    868. rangeCheckForAdd(index);
    869. finalint offset =this.offset;
    870. returnnewListIterator<E>(){
    871. int cursor = index;
    872. int lastRet =-1;
    873. int expectedModCount =ArrayList.this.modCount;
    874. publicboolean hasNext(){
    875. return cursor !=SubList.this.size;
    876. }
    877. @SuppressWarnings("unchecked")
    878. public E next(){
    879. checkForComodification();
    880. int i = cursor;
    881. if(i >=SubList.this.size)
    882. thrownewNoSuchElementException();
    883. Object[] elementData =ArrayList.this.elementData;
    884. if(offset + i >= elementData.length)
    885. thrownewConcurrentModificationException();
    886. cursor = i +1;
    887. return(E) elementData[offset +(lastRet = i)];
    888. }
    889. publicboolean hasPrevious(){
    890. return cursor !=0;
    891. }
    892. @SuppressWarnings("unchecked")
    893. public E previous(){
    894. checkForComodification();
    895. int i = cursor -1;
    896. if(i <0)
    897. thrownewNoSuchElementException();
    898. Object[] elementData =ArrayList.this.elementData;
    899. if(offset + i >= elementData.length)
    900. thrownewConcurrentModificationException();
    901. cursor = i;
    902. return(E) elementData[offset +(lastRet = i)];
    903. }
    904. //对未迭代的元素进行特定的操作,并将游标移到迭代完成的位置
    905. @SuppressWarnings("unchecked")
    906. publicvoid forEachRemaining(Consumer<?super E> consumer){
    907. Objects.requireNonNull(consumer);
    908. finalint size =SubList.this.size;
    909. int i = cursor;
    910. if(i >= size){
    911. return;
    912. }
    913. finalObject[] elementData =ArrayList.this.elementData;
    914. if(offset + i >= elementData.length){
    915. thrownewConcurrentModificationException();
    916. }
    917. while(i != size && modCount == expectedModCount){
    918. consumer.accept((E) elementData[offset +(i++)]);
    919. }
    920. //更新游标
    921. lastRet = cursor = i;
    922. checkForComodification();
    923. }
    924. publicint nextIndex(){
    925. return cursor;
    926. }
    927. publicint previousIndex(){
    928. return cursor -1;
    929. }
    930. publicvoid remove(){
    931. if(lastRet <0)
    932. thrownewIllegalStateException();
    933. checkForComodification();
    934. try{
    935. SubList.this.remove(lastRet);
    936. cursor = lastRet;
    937. lastRet =-1;
    938. expectedModCount =ArrayList.this.modCount;
    939. }catch(IndexOutOfBoundsException ex){
    940. thrownewConcurrentModificationException();
    941. }
    942. }
    943. publicvoidset(E e){
    944. if(lastRet <0)
    945. thrownewIllegalStateException();
    946. checkForComodification();
    947. try{
    948. ArrayList.this.set(offset + lastRet, e);
    949. }catch(IndexOutOfBoundsException ex){
    950. thrownewConcurrentModificationException();
    951. }
    952. }
    953. publicvoid add(E e){
    954. checkForComodification();
    955. try{
    956. int i = cursor;
    957. SubList.this.add(i, e);
    958. cursor = i +1;
    959. lastRet =-1;
    960. expectedModCount =ArrayList.this.modCount;
    961. }catch(IndexOutOfBoundsException ex){
    962. thrownewConcurrentModificationException();
    963. }
    964. }
    965. finalvoid checkForComodification(){
    966. if(expectedModCount !=ArrayList.this.modCount)
    967. thrownewConcurrentModificationException();
    968. }
    969. };
    970. }
    971. //从当前子序列获得子序列,offset当前子列表位于最原始的列表的偏移
    972. publicList<E> subList(int fromIndex,int toIndex){
    973. subListRangeCheck(fromIndex, toIndex, size);
    974. returnnewSubList(this, offset, fromIndex, toIndex);
    975. }
    976. //检查当前索引的范围是否正确
    977. privatevoid rangeCheck(int index){
    978. if(index <0|| index >=this.size)
    979. thrownewIndexOutOfBoundsException(outOfBoundsMsg(index));
    980. }
    981. //检查当前索引的范围是否正确
    982. privatevoid rangeCheckForAdd(int index){
    983. if(index <0|| index >this.size)
    984. thrownewIndexOutOfBoundsException(outOfBoundsMsg(index));
    985. }
    986. //生成索引越界错误消息
    987. privateString outOfBoundsMsg(int index){
    988. return"Index: "+index+", Size: "+this.size;
    989. }
    990. //判断是否有修改
    991. privatevoid checkForComodification(){
    992. if(ArrayList.this.modCount !=this.modCount)
    993. thrownewConcurrentModificationException();
    994. }
    995. publicSpliterator<E> spliterator(){
    996. checkForComodification();
    997. //子列表的已经不是第一次使用
    998. //fence 为父列表中属于子列表的最后一个元素的索引
    999. returnnewArrayListSpliterator<E>(ArrayList.this, offset,
    1000. offset +this.size,this.modCount);
    1001. }
    1002. }
    1003. /**
    1004. * 对数组中的元素执行特定的操作,不允许在此过程中修改
    1005. */
    1006. @Override
    1007. publicvoid forEach(Consumer<?super E> action){
    1008. Objects.requireNonNull(action);
    1009. finalint expectedModCount = modCount;
    1010. @SuppressWarnings("unchecked")
    1011. final E[] elementData =(E[])this.elementData;
    1012. finalint size =this.size;
    1013. for(int i=0; modCount == expectedModCount && i < size; i++){
    1014. action.accept(elementData[i]);
    1015. }
    1016. if(modCount != expectedModCount){
    1017. thrownewConcurrentModificationException();
    1018. }
    1019. }
    1020. /**
    1021. * 在列表上创建一个延迟绑定和fail-fast机制
    1022. *
    1023. * Spliterator提供列表的遍历和元素分割
    1024. *
    1025. * @return a {@code Spliterator} over the elements in this list
    1026. * @since 1.8
    1027. */
    1028. @Override
    1029. publicSpliterator<E> spliterator(){
    1030. returnnewArrayListSpliterator<>(this,0,-1,0);
    1031. }
    1032. /** Index-based split-by-two, lazily initialized Spliterator */
    1033. staticfinalclassArrayListSpliterator<E>implementsSpliterator<E>{
    1034. /*
    1035. * 实在看不懂 ==!
    1036. * 如果ArrayList是不可变或者结构上不可变的(adds,removs等),可以使用
    1037. * Arrays.spliterator来实现。遍历过程中,不牺牲太多性能的情况下,我们可以检测到
    1038. * 干扰。(不太通,Instead we detect as much interference during traversal
    1039. * as practical without sacrificing much performance.) 我们主要依赖于
    1040. * modCounts。虽然这并不能保证检查到并发访问冲突,而且有时对于线程间的干扰过于保
    1041. * 守,但是发现足够的问题在实践中是值得的。为了实现这个功能,做了以下几点:
    1042. * 1. 延迟初始化fence和expectedModCount直到需要我们提交我们正在检测的状态信息
    1043. * 以此提高精确性(这不适用于子列表,子列表使用非延迟加载值)
    1044. * 2. 只在forEach结束时检测一次ConcurrentModificationException异常(性能最
    1045. * 敏感的方法)。使用forEach(与迭代器相反),我们通常只能在action操作之后检测
    1046. * 干扰,而不是开始之前。因为干扰的原因,假设如null或者列表元素很小,更进一步的
    1047. * CME-triggering检测将应用于所有的可能破外。这允许forEach内部的循环在运行时不
    1048. * 进行进一步检查,简化lambda表达式的操作。虽然这确实需要大量的检查(
    1049. * list.stream().forEach(a)相同的情况), 没有其他的检测和计算发生在forEach内,
    1050. * 其他不常使用的方法不能从流中获得大部分性能
    1051. */
    1052. privatefinalArrayList<E> list;
    1053. privateint index;// 当前的索引(advance/split操作的对象)
    1054. privateint fence;// -1 until used; then one past last index
    1055. privateint expectedModCount;// 当fence设置后初始化
    1056. /** 根据范围创建一个Spliterator */
    1057. ArrayListSpliterator(ArrayList<E> list,int origin,int fence,
    1058. int expectedModCount){
    1059. this.list = list;// 可以是null(除非需要遍历)
    1060. this.index = origin;
    1061. this.fence = fence;
    1062. this.expectedModCount = expectedModCount;
    1063. }
    1064. privateint getFence(){// 第一次使用时,初始化fence为size
    1065. int hi;// (a specialized variant appears in method forEach)
    1066. ArrayList<E> lst;
    1067. if((hi = fence)<0){//第一次使用,则初始化
    1068. if((lst = list)==null)
    1069. hi = fence =0;
    1070. else{
    1071. expectedModCount = lst.modCount;//初始化expectedModCount
    1072. hi = fence = lst.size;
    1073. }
    1074. }
    1075. return hi;
    1076. }
    1077. publicArrayListSpliterator<E> trySplit(){
    1078. //求中间索引,>>>无符号右移,高位补0
    1079. int hi = getFence(), lo = index, mid =(lo + hi)>>>1;
    1080. return(lo >= mid)?null:// 分成两份,如果列表太小返回null
    1081. newArrayListSpliterator<E>(list, lo, index = mid,
    1082. expectedModCount);
    1083. }
    1084. //执行特定操作,用于遍历,每调用一次,index增大,知道fence(也即到列表末尾)
    1085. publicboolean tryAdvance(Consumer<?super E> action){
    1086. if(action ==null)
    1087. thrownewNullPointerException();
    1088. int hi = getFence(), i = index;
    1089. if(i < hi){
    1090. index = i +1;
    1091. @SuppressWarnings("unchecked") E e =(E)list.elementData[i];
    1092. action.accept(e);
    1093. if(list.modCount != expectedModCount)
    1094. thrownewConcurrentModificationException();
    1095. returntrue;
    1096. }
    1097. returnfalse;
    1098. }
    1099. //遍历执行特定操作
    1100. publicvoid forEachRemaining(Consumer<?super E> action){
    1101. int i, hi, mc;// hoist accesses and checks from loop
    1102. ArrayList<E> lst;Object[] a;
    1103. if(action ==null)
    1104. thrownewNullPointerException();
    1105. if((lst = list)!=null&&(a = lst.elementData)!=null){
    1106. if((hi = fence)<0){
    1107. mc = lst.modCount;
    1108. hi = lst.size;
    1109. }
    1110. else
    1111. mc = expectedModCount;
    1112. if((i = index)>=0&&(index = hi)<= a.length){
    1113. for(; i < hi;++i){
    1114. @SuppressWarnings("unchecked") E e =(E) a[i];
    1115. action.accept(e);
    1116. }
    1117. if(lst.modCount == mc)
    1118. return;
    1119. }
    1120. }
    1121. thrownewConcurrentModificationException();
    1122. }
    1123. //获得还剩余的未遍历的元素数
    1124. publiclong estimateSize(){
    1125. return(long)(getFence()- index);
    1126. }
    1127. //Spliterator特征
    1128. publicint characteristics(){
    1129. returnSpliterator.ORDERED |Spliterator.SIZED |Spliterator.SUBSIZED;
    1130. }
    1131. }
    1132. /**
    1133. * 根据条件移除元素,元素有移除,则返回true
    1134. */
    1135. @Override
    1136. publicboolean removeIf(Predicate<?super E> filter){
    1137. Objects.requireNonNull(filter);
    1138. // figure out which elements are to be removed
    1139. // any exception thrown from the filter predicate at this stage
    1140. // will leave the collection unmodified
    1141. int removeCount =0;
    1142. finalBitSet removeSet =newBitSet(size);
    1143. finalint expectedModCount = modCount;
    1144. finalint size =this.size;
    1145. for(int i=0; modCount == expectedModCount && i < size; i++){
    1146. @SuppressWarnings("unchecked")
    1147. final E element =(E) elementData[i];
    1148. if(filter.test(element)){
    1149. removeSet.set(i);//设置第i位为1,标记移除
    1150. removeCount++;//记录移除的记录数
    1151. }
    1152. }
    1153. //此过程中发生列表结构修改,抛出异常
    1154. if(modCount != expectedModCount){
    1155. thrownewConcurrentModificationException();
    1156. }
    1157. // 将没有移除的元素向前移动
    1158. finalboolean anyToRemove = removeCount >0;
    1159. if(anyToRemove){
    1160. finalint newSize = size - removeCount;
    1161. for(int i=0, j=0;(i < size)&&(j < newSize); i++, j++){
    1162. //找到i之后第一个为false的index,也即未标记移除的
    1163. i = removeSet.nextClearBit(i);
    1164. elementData[j]= elementData[i];
    1165. }
    1166. for(int k=newSize; k < size; k++){
    1167. elementData[k]=null;//清除需要删除的引用,以让垃圾回收器回收
    1168. }
    1169. this.size = newSize;//更新尺寸
    1170. //此过程中发生列表结构修改,抛出异常
    1171. if(modCount != expectedModCount){
    1172. thrownewConcurrentModificationException();
    1173. }
    1174. modCount++;
    1175. }
    1176. //移除元素,则返回true
    1177. return anyToRemove;
    1178. }
    1179. /**
    1180. * 对列表中每一个元素根据传入的UnaryOperator,执行特定操作,并用返回值替换原来的值
    1181. */
    1182. @Override
    1183. @SuppressWarnings("unchecked")
    1184. publicvoid replaceAll(UnaryOperator<E>operator){
    1185. Objects.requireNonNull(operator);
    1186. finalint expectedModCount = modCount;
    1187. finalint size =this.size;
    1188. for(int i=0; modCount == expectedModCount && i < size; i++){
    1189. //执行特定操作,并用返回值替换原来的值
    1190. elementData[i]=operator.apply((E) elementData[i]);
    1191. }
    1192. if(modCount != expectedModCount){
    1193. thrownewConcurrentModificationException();
    1194. }
    1195. modCount++;
    1196. }
    1197. //根据传入的比较器c对列表进行排序
    1198. @Override
    1199. @SuppressWarnings("unchecked")
    1200. publicvoid sort(Comparator<?super E> c){
    1201. finalint expectedModCount = modCount;
    1202. Arrays.sort((E[]) elementData,0, size, c);
    1203. //排序过程中发生列表结构变化,则抛出异常
    1204. if(modCount != expectedModCount){
    1205. thrownewConcurrentModificationException();
    1206. }
    1207. modCount++;
    1208. }
    1209. }

    ArrayList 中的很大部分操作,使用了Arrays.copyof()和System.arraycopy()进行数组的拷贝,需要进一步分析其源码
    列表的排序使用Arrays.sort(),进一步分析其源码





  • 相关阅读:
    Java多线程系列--“基础篇”11之 生产消费者问题
    Java多线程系列--“基础篇”10之 线程优先级和守护线程
    Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
    Java多线程系列--“基础篇”08之 join()
    Java四种线程池的使用
    数据库索引的实现原理
    Java多线程系列--“基础篇”07之 线程休眠
    Java多线程系列--“基础篇”06之 线程让步
    Java多线程系列--“基础篇”05之 线程等待与唤醒
    Java多线程系列--“基础篇”04之 synchronized关键字
  • 原文地址:https://www.cnblogs.com/ggmfengyangdi/p/5738491.html
Copyright © 2020-2023  润新知