原文链接:https://github.com/Snailclimb/JavaGuide 
 
ArrayList简介   ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增长。在添加大量元素前,应用程序可以使用ensureCapacity操作来增加 ArrayList 实例的容量。这可以减少递增式再分配的数量。
   它继承于 AbstractList ,实现了 List , RandomAccess , Cloneable , java.io.Serializable  这些接口。
   在我们学数据结构的时候就知道了线性表的顺序存储,插入删除元素的时间复杂度为O(n) ,求表长以及增加元素,取第 i   元素的时间复杂度为O(1) 
   ArrayList 继承了AbstractList,实现了List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
  ArrayList 实现了RandomAccess 接口 , RandomAccess 是一个标志接口,表明实现这个这个接口的 List 集合是支持快速随机访问 的。在 ArrayList 中,我们即可以通过元素的序号快速获取元素对象,这就是快速随机访问。
  ArrayList 实现了Cloneable 接口 ,即覆盖了函数 clone(),能被克隆 。
  ArrayList 实现java.io.Serializable 接口 ,这意味着ArrayList支持序列化 ,能通过序列化去传输 。
  和 Vector 不同,ArrayList 中的操作不是线程安全的 !所以,建议在单线程中才使用 ArrayList,而在多线程中可以选择 Vector 或者  CopyOnWriteArrayList。
ArrayList核心源码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 package  java.util;import  java.util.function.Consumer;import  java.util.function.Predicate;import  java.util.function.UnaryOperator;public  class  ArrayList <E > extends  AbstractList <E >        implements  List <E >, RandomAccess , Cloneable , java .io .Serializable      private  static  final  long  serialVersionUID = 8683452581122892189L ;          private  static  final  int  DEFAULT_CAPACITY = 10 ;          private  static  final  Object[] EMPTY_ELEMENTDATA = {};                  private  static  final  Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};          transient  Object[] elementData;           private  int  size;          public  ArrayList (int  initialCapacity)           if  (initialCapacity > 0 ) {                          this .elementData = new  Object[initialCapacity];         } else  if  (initialCapacity == 0 ) {                          this .elementData = EMPTY_ELEMENTDATA;         } else  {             throw  new  IllegalArgumentException("Illegal Capacity: " +                                                initialCapacity);         }     }          public  ArrayList ()           this .elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;     }          public  ArrayList (Collection<? extends E> c)                    elementData = c.toArray();                  if  ((size = elementData.length) != 0 ) {                                       if  (elementData.getClass() != Object[].class )                  elementData   = Arrays.copyOf(elementData, size, Object[].class ) ;        } else  {                          this .elementData = EMPTY_ELEMENTDATA;         }     }          public  void  trimToSize ()           modCount++;         if  (size < elementData.length) {             elementData = (size == 0 )               ? EMPTY_ELEMENTDATA               : Arrays.copyOf(elementData, size);         }     }          public  void  ensureCapacity (int  minCapacity)           int  minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)                          ? 0                                        : DEFAULT_CAPACITY;         if  (minCapacity > minExpand) {             ensureExplicitCapacity(minCapacity);         }     }         private  void  ensureCapacityInternal (int  minCapacity)           if  (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {                            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);         }         ensureExplicitCapacity(minCapacity);     }        private  void  ensureExplicitCapacity (int  minCapacity)           modCount++;                  if  (minCapacity - elementData.length > 0 )                          grow(minCapacity);     }          private  static  final  int  MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8 ;          private  void  grow (int  minCapacity)                    int  oldCapacity = elementData.length;                           int  newCapacity = oldCapacity + (oldCapacity >> 1 );                  if  (newCapacity - minCapacity < 0 )             newCapacity = minCapacity;                                    if  (newCapacity - MAX_ARRAY_SIZE > 0 )             newCapacity = hugeCapacity(minCapacity);                  elementData = Arrays.copyOf(elementData, newCapacity);     }          private  static  int  hugeCapacity (int  minCapacity)           if  (minCapacity < 0 )              throw  new  OutOfMemoryError();         return  (minCapacity > MAX_ARRAY_SIZE) ?             Integer.MAX_VALUE :             MAX_ARRAY_SIZE;     }          public  int  size ()           return  size;     }          public  boolean  isEmpty ()                    return  size == 0 ;     }          public  boolean  contains (Object o)                    return  indexOf(o) >= 0 ;     }          public  int  indexOf (Object o)           if  (o == null ) {             for  (int  i = 0 ; i < size; i++)                 if  (elementData[i]==null )                     return  i;         } else  {             for  (int  i = 0 ; i < size; i++)                                  if  (o.equals(elementData[i]))                     return  i;         }         return  -1 ;     }          public  int  lastIndexOf (Object o)           if  (o == null ) {             for  (int  i = size-1 ; i >= 0 ; i--)                 if  (elementData[i]==null )                     return  i;         } else  {             for  (int  i = size-1 ; i >= 0 ; i--)                 if  (o.equals(elementData[i]))                     return  i;         }         return  -1 ;     }          public  Object clone ()           try  {             ArrayList<?> v = (ArrayList<?>) super .clone();                          v.elementData = Arrays.copyOf(elementData, size);             v.modCount = 0 ;             return  v;         } catch  (CloneNotSupportedException e) {                          throw  new  InternalError(e);         }     }          public  Object[] toArray() {         return  Arrays.copyOf(elementData, size);     }          @SuppressWarnings ("unchecked" )     public  <T> T[] toArray(T[] a) {         if  (a.length < size)                          return  (T[]) Arrays.copyOf(elementData, size, a.getClass());                      System.arraycopy(elementData, 0 , a, 0 , size);         if  (a.length > size)             a[size] = null ;         return  a;     }          @SuppressWarnings ("unchecked" )     E elementData (int  index)   {         return  (E) elementData[index];     }          public  E get (int  index)           rangeCheck(index);         return  elementData(index);     }          public  E set (int  index, E element)                    rangeCheck(index);         E oldValue = elementData(index);         elementData[index] = element;                  return  oldValue;     }          public  boolean  add (E e)           ensureCapacityInternal(size + 1 );                    elementData[size++] = e;         return  true ;     }          public  void  add (int  index, E element)           rangeCheckForAdd(index);         ensureCapacityInternal(size + 1 );                    System.arraycopy(elementData, index, elementData, index + 1 ,                          size - index);         elementData[index] = element;         size++;     }          public  E remove (int  index)           rangeCheck(index);         modCount++;         E oldValue = elementData(index);         int  numMoved = size - index - 1 ;         if  (numMoved > 0 )             System.arraycopy(elementData, index+1 , elementData, index,                              numMoved);         elementData[--size] = null ;                 return  oldValue;     }          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  void  fastRemove (int  index)           modCount++;         int  numMoved = size - index - 1 ;         if  (numMoved > 0 )             System.arraycopy(elementData, index+1 , elementData, index,                              numMoved);         elementData[--size] = null ;      }          public  void  clear ()           modCount++;                  for  (int  i = 0 ; i < size; i++)             elementData[i] = null ;         size = 0 ;     }          public  boolean  addAll (Collection<? extends E> c)           Object[] a = c.toArray();         int  numNew = a.length;         ensureCapacityInternal(size + numNew);           System.arraycopy(a, 0 , elementData, size, numNew);         size += numNew;         return  numNew != 0 ;     }          public  boolean  addAll (int  index, Collection<? extends E> c)           rangeCheckForAdd(index);         Object[] a = c.toArray();         int  numNew = a.length;         ensureCapacityInternal(size + numNew);           int  numMoved = size - index;         if  (numMoved > 0 )             System.arraycopy(elementData, index, elementData, index + numNew,                              numMoved);         System.arraycopy(a, 0 , elementData, index, numNew);         size += numNew;         return  numNew != 0 ;     }          protected  void  removeRange (int  fromIndex, int  toIndex)           modCount++;         int  numMoved = size - toIndex;         System.arraycopy(elementData, toIndex, elementData, fromIndex,                          numMoved);                  int  newSize = size - (toIndex-fromIndex);         for  (int  i = newSize; i < size; i++) {             elementData[i] = null ;         }         size = newSize;     }          private  void  rangeCheck (int  index)           if  (index >= size)             throw  new  IndexOutOfBoundsException(outOfBoundsMsg(index));     }          private  void  rangeCheckForAdd (int  index)           if  (index > size || index < 0 )             throw  new  IndexOutOfBoundsException(outOfBoundsMsg(index));     }          private  String outOfBoundsMsg (int  index)           return  "Index: " +index+", Size: " +size;     }          public  boolean  removeAll (Collection<?> c)           Objects.requireNonNull(c);                  return  batchRemove(c, false );     }          public  boolean  retainAll (Collection<?> c)           Objects.requireNonNull(c);         return  batchRemove(c, true );     }          public  ListIterator<E> listIterator (int  index)           if  (index < 0  || index > size)             throw  new  IndexOutOfBoundsException("Index: " +index);         return  new  ListItr(index);     }          public  ListIterator<E> listIterator ()           return  new  ListItr(0 );     }          public  Iterator<E> iterator ()           return  new  Itr();     } 
ArrayList源码分析 System.arraycopy()和Arrays.copyOf()方法   通过上面源码我们发现这两个实现数组复制的方法被广泛使用而且很多地方都特别巧妙。比如下面add(int index, E element) 方法就很巧妙的用到了arraycopy()方法 让数组自己复制自己实现让index开始之后的所有成员后移一个位置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public  void  add (int  index, E element)      rangeCheckForAdd(index);     ensureCapacityInternal(size + 1 );                 System.arraycopy(elementData, index, elementData, index + 1 , size - index);     elementData[index] = element;     size++; } 
又如toArray()方法中用到了copyOf()方法
1 2 3 4 5 6 7 8 9 10 public  Object[] toArray() {    return  Arrays.copyOf(elementData, size); } 
两者联系与区别 联系: copyOf()内部调用了System.arraycopy()方法区别: 
arraycopy()需要目标数组,将原数组拷贝到你自己定义的数组里,而且可以选择拷贝的起点和长度以及放入新数组中的位置 
copyOf()是系统自动在内部新建一个数组,并返回该数组。ArrayList 核心扩容技术 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38          public  void  ensureCapacity (int  minCapacity)           int  minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)                          ? 0                                        : DEFAULT_CAPACITY;         if  (minCapacity > minExpand) {             ensureExplicitCapacity(minCapacity);         }     }         private  void  ensureCapacityInternal (int  minCapacity)           if  (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {                            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);         }         ensureExplicitCapacity(minCapacity);     }        private  void  ensureExplicitCapacity (int  minCapacity)           modCount++;                           if  (minCapacity - elementData.length > 0 )                          grow(minCapacity);     } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 private  void  grow (int  minCapacity)                   int  oldCapacity = elementData.length;               int  newCapacity = oldCapacity + (oldCapacity >> 1 );          if  (newCapacity - minCapacity < 0 )         newCapacity = minCapacity;                    if  (newCapacity - MAX_ARRAY_SIZE > 0 )         newCapacity = hugeCapacity(minCapacity);          elementData = Arrays.copyOf(elementData, newCapacity); } 
移位运算符 简介 :移位运算符就是在二进制的基础上对数字进行平移。按照平移的方向和填充数字的规则分为三种:<<(左移) 、>>(带符号右移) 和>>>(无符号右移) 。作用 :对于大数据的2进制运算,位移运算符比那些普通运算符的运算要快很多,因为程序仅仅移动一下而已,不去计算,这样提高了效率,节省了资源  
 
另外需要注意的是: 
java 中的length 属性 是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了 length 这个属性.
java 中的length()方法 是针对字  符串String说的,如果想看这个字符串的长度则用到 length()这个方法.
.java 中的size()方法 是针对泛型集合说的,如果想看这个泛型有多少个元素,就调用此方法来查看!
 
内部类 1 2 3 4 (1 )private  class  Itr  implements  Iterator <E >   (2)private  class  ListItr  extends  Itr  implements  ListIterator <E >   (3)private  class  SubList  extends  AbstractList <E > implements  RandomAccess    (4)static  final  class  ArrayListSpliterator <E > implements  Spliterator <E > 
  ArrayList有四个内部类,其中的Itr是实现了Iterator接口 ,同时重写了里面的hasNext() , next() , remove()  等方法;其中的ListItr  继承 Itr ,实现了ListIterator接口 ,同时重写了hasPrevious() , nextIndex() , previousIndex() , previous() , set(E e) , add(E e)  等方法,所以这也可以看出了 Iterator和ListIterator的区别:  ListIterator在Iterator的基础上增加了添加对象,修改对象,逆向遍历等方法,这些是Iterator不能实现的。
 ArrayList经典Demo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 package  list;import  java.util.ArrayList;import  java.util.Iterator;public  class  ArrayListDemo      public  static  void  main (String[] srgs)           ArrayList<Integer> arrayList = new  ArrayList<Integer>();          System.out.printf("Before add:arrayList.size() = %d\n" ,arrayList.size());          arrayList.add(1 );          arrayList.add(3 );          arrayList.add(5 );          arrayList.add(7 );          arrayList.add(9 );          System.out.printf("After add:arrayList.size() = %d\n" ,arrayList.size());          System.out.println("Printing elements of arrayList" );                              System.out.print("通过迭代器遍历:" );          Iterator<Integer> it = arrayList.iterator();          while (it.hasNext()){              System.out.print(it.next() + " " );          }          System.out.println();                    System.out.print("通过索引值遍历:" );          for (int  i = 0 ; i < arrayList.size(); i++){              System.out.print(arrayList.get(i) + " " );          }          System.out.println();                    System.out.print("for循环遍历:" );          for (Integer number : arrayList){              System.out.print(number + " " );          }                              Integer[] integer = arrayList.toArray(new  Integer[0 ]);                    Integer[] integer1 = new  Integer[arrayList.size()];          arrayList.toArray(integer1);                                        System.out.println();                    arrayList.add(2 ,2 );                    arrayList.remove(2 );                        arrayList.remove((Object)3 );                    System.out.println("ArrayList contains 5 is: "  + arrayList.contains(5 ));                    arrayList.clear();                    System.out.println("ArrayList is empty: "  + arrayList.isEmpty());     } }