集合详解

面试中基础最常问的集合问题,这里将集合重新梳理下,java中的集合是引用java.util包中的集合对象,用于存储对象使用。

集合:

1.集合里可以放各种类型;

2.集合长度可以自动扩容

3.集合中只能放引入类型,不能放基本类型。

collection

迭代器:iterator()

while( iter.hasNext() ){迭代器对象判断集合中下一个元素是否存在

  iter.next();

}

一、ArrayList

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable

可以看到ArryList是实现了List集合,继承了AbstractList。并且初始容器大小为10,当新增数据超过给定的容量大小时,会扩容1.5倍。

private static final int DEFAULT_CAPACITY = 10;
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        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);
    }

二、LinkedList

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

  

  

 

 

posted @ 2018-09-20 22:34  isOkFine  阅读(137)  评论(0编辑  收藏  举报