Java集合复习之List

2018-11-14  本文已影响0人  冷江明

1.ArrayList

1.1.ArrayList集合特点
1.2 ArrayList排序
1.3 ArrayList扩容
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

private void grow(int minCapacity) {
        // oldCapacity 是旧容量,newCapacity 是新容量
        int oldCapacity = elementData.length;
        //这里进行了扩容,通过位运算,将旧容量扩大为原来的1.5倍
        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);
    }

 private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }
1.4 ArrayList序列化
//序列化
ArrayList list = new ArrayList();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);

//反序列化
ArrayList list = new ArrayList();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ois.readObject(list);

2.Vector

2.1.Vector集合特点

3.LinkedList

3.1.LinkedList集合特点

4.ArrayList、Vector、LinkedList对比

5.list的使用场景分析

上一篇 下一篇

猜你喜欢

热点阅读