java-集合1

1.Iterator接口,有hasNext(),next(),remove()方法

 

2.List 接口继承Collection ,Collection接口 继承 Iterable

  a.ArrayList

  

//一个Object数组,其中transient定义为不能序列化
private transient Object[] elementData;
//数组的长度
 private int size;


// 添加元素完后,size+1,

public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

 

private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code

// 判断当前容量是否足够,如果不够,就扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

 

private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;

//新容量大小等于 旧容量+旧容量/2
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}

 

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);

//通过此方法拷贝一个新的对象数组,
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

// 每一个实现类都实现了一个自己的迭代器

private class Itr implements Iterator<E> 

 

 

 

 

 

 



 

      b.LinkedList 双向链表

posted @ 2017-04-21 13:26  随心2017  阅读(125)  评论(0编辑  收藏  举报