标签:
常用
1 ArrayList 动态数组
1 /** 2 * Default initial capacity. 3 */ 4 private static final int DEFAULT_CAPACITY = 10; 5 6 public ArrayList(int initialCapacity) { 7 super(); 8 if (initialCapacity < 0) 9 throw new IllegalArgumentException("Illegal Capacity: "+ 10 initialCapacity); 11 this.elementData = new Object[initialCapacity]; 12 } 13 14 public ArrayList() { 15 super(); 16 this.elementData = EMPTY_ELEMENTDATA; 17 } 18 19 public ArrayList(Collection<? extends E> c) { 20 elementData = c.toArray(); 21 size = elementData.length; 22 // c.toArray might (incorrectly) not return Object[] (see 6260652) 23 if (elementData.getClass() != Object[].class) 24 elementData = Arrays.copyOf(elementData, size, Object[].class); 25 } 26 27 public boolean add(E e) { 28 ensureCapacityInternal(size + 1); // Increments modCount!! 29 elementData[size++] = e; 30 return true; 31 } 32 33 public E get(int index) { 34 rangeCheck(index); 35 36 return elementData(index); 37 }
标签:
原文地址:http://www.cnblogs.com/jason-Z/p/4930802.html