java--19--List及其实现类与子接口

来源:互联网 发布:现代风格家具品牌 知乎 编辑:程序博客网 时间:2024/05/24 00:50

1 List接口
1.1 重要方法:

//返回迭代器,可以来遍历集合。Iterator<E> iterator();//将List集合返回为一个Object数组<T> T[] toArray(T[] a);例:ArrayList<Integer> lst=new ArrayList<Integer>();        lst.add(1);        lst.add(2);        Object[] intlst=new Object[2];         intlst=lst.toArray();        System.out.println((Integer)intlst[0]);//输出1//仅保留集合中包含指定集合中的元素的元素,剔除集合中的元素不在参数集合中的元素。//即去两个集合的交集。boolean retainAll(Collection<?> c);

2 LinkList实现List接口,可以存储null至其中。有两个指针,一个指向第一个元素,一个指向最后的元素。

/**     * Pointer to first node.     * Invariant: (first == null && last == null) ||     *            (first.prev == null && first.item != null)     */    transient Node<E> first;    /**     * Pointer to last node.     * Invariant: (first == null && last == null) ||     *            (last.next == null && last.item != null)     */    transient Node<E> last;

每一个元素的结构为一个指向前和指向后的指针。类似于数据结构中的双链表。

private static class Node<E> {        E item;        Node<E> next;        Node<E> prev;        Node(Node<E> prev, E element, Node<E> next) {            this.item = element;            this.next = next;            this.prev = prev;        }    }头插法:/**     * Links e as first element.     */    private void linkFirst(E e) {        final Node<E> f = first;        final Node<E> newNode = new Node<>(null, e, f);        first = newNode;        if (f == null)            last = newNode;//插入之前是空,头尾指针均指向该节点        else            f.prev = newNode;//更新之前的头指针指向前的位置        size++;        modCount++;    }尾插法:    /**     * Links e as last element.     */    void linkLast(E e) {        final Node<E> l = last;        final Node<E> newNode = new Node<>(l, e, null);        last = newNode;        if (l == null)            first = newNode;//插入之前为空,头尾指针均指向它        else            l.next = newNode;//不要空,更新之前的尾指针的尾指向        size++;        modCount++;    }

3 ArrayList
3.1 Resizable-array implementation of the List interface. 可变数组实现List接口, Implements all optional list operations,实现List所有的操作 and permits all elements, 适合所有的类似including null.包括为空 In addition to implementing the List interface除了实现List接口,this class provides methods to manipulate the size of the array that is used internally to store the list.这个类提供了操作数组大小的 方法 (This class is roughly equivalent to Vector, except that it is unsynchronized.这个类和Vector类似,但不是同步安全。
3.2 重要方法:

//根据集合来初始化ArrayList集合、public ArrayList(Collection<? extends E> c) {        elementData = c.toArray();        size = elementData.length;        // c.toArray might (incorrectly) not return Object[] (see 6260652)        if (elementData.getClass() != Object[].class)            elementData = Arrays.copyOf(elementData, size, Object[].class);    }下面是上面Arrays.copyOf的解释。    //初始化数组,复制指定段的数据至数组中,第一个为被复制数组,第二个参数为复制的长度,第三个参数为数组类型,返回粘贴后的数组,即新创立的数组。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;    }//从置ArrayList结构中的元素。调用了System.copy,去除掉null的元素,public void trimToSize();//自增ArrayList空间大小//参数minCapacity为增加的最小大小private void grow(int minCapacity) {        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);//元素复制}//去除集合中包含指定集合中的元素public boolean removeAll(Collection<?> c) //取并集public boolean retainAll(Collection<?> c)

3.2 数据用private Object[]存储

4 Vector
4.1 类说明:实现List接口,继承AbstractList,类中用数组的形式存储数据,Vector中的方法大部分都是线程安全。

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the code Vector has been created.
4.2 重要方法:
迭代遍历输出元素:

ListIterator e2 = ((List) o).listIterator();        while (e1.hasNext() && e2.hasNext()) {            E o1 = e1.next();            Object o2 = e2.next();            if (!(o1==null ? o2==null : o1.equals(o2)))                return false;        }
0 0
原创粉丝点击