2018-10-02容器、尝试写ArrayList-数组扩容、r

2018-10-09  本文已影响0人  消失的七月

1.

.Collection接口: Collection表示一组对象,他是集中收集的意思,就是把一组数据收集起来

Collection接口的两个子接口Set,List:

    -Set中的数据没有顺序,不可以重复。

    -List中的数据有顺序,可以重复。

Map 接口定义了存储“键<key> - 值<value>”

2.自写的ArraListy数组扩容add

public void add(Object o) {

//数组扩容

if(size >= element.length) {

    Object newArray = new Object[size*2];

        System.arraycopy(element, 0, newArray, 0, size);

    element = newArray;

    }

    element[size] = o;

    size++;

    }

}

ArrayList,LinkedList,Vector

ArrayList:底层实现是数组。线程不安全,效率高。所以查询快,修改、删除慢;

LinkedList:底层实现是链表。线程不安全,效率高。所以查询慢。修改、插入删除快;

Vector:线程安全的,效率低

ArrayList的remove:

public void remove(int index){

//删除指定位置对象

//a b d e

int numMoved = size - index - 1;

if(numMoved > 0){

    System.arraycopy(elementDate, index+1, elementDate, index, numMoved);

}

    elementDate[--size] = null;

}

上一篇 下一篇

猜你喜欢

热点阅读