zoukankan      html  css  js  c++  java
  • Java容器jdk1.6 Array

    参考:https://www.cnblogs.com/tstd/p/5042087.html

    1.定义

    顶层接口collection

    public interface Collection<E> extends Iterable<E> {
        int size();
        boolean isEmpty();
        boolean contains(Object o);
        Iterator<E> iterator();
        Object[] toArray();
        <T> T[] toArray(T[] a);
        boolean add(E e);
        boolean remove(Object o);
        boolean containsAll(Collection<?> c);
        boolean addAll(Collection<? extends E> c);
        boolean removeAll(Collection<?> c);
        boolean retainAll(Collection<?> c);
        void clear();
        boolean equals(Object o);
        int hashCode();
    }
    

      List定义

    public interface List<E> extends Collection<E> {
             int size();
             boolean isEmpty();
             boolean contains(Object o);
             Iterator<E> iterator();
             Object[] toArray();
             <T> T[] toArray(T[] a);
             boolean add(E e);
             boolean remove(Object o);
             boolean containsAll(Collection<?> c);
             boolean addAll(Collection<? extends E> c);
             boolean addAll( int index, Collection<? extends E> c);
             boolean removeAll(Collection<?> c);
             boolean retainAll(Collection<?> c);
             void clear();
             boolean equals(Object o);
             int hashCode();
             E get( int index);
             E set( int index, E element);
             void add( int index, E element);
             E remove( int index);
             int indexOf(Object o);
             int lastIndexOf(Object o);
             ListIterator<E> listIterator();
             ListIterator<E> listIterator( int index);
             List<E> subList( int fromIndex, int toIndex);
    }
    

      ArrayList

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

      RandomAccess是一个标记接口,用来表明其支持快速随机访问

      AbstractList(这是一个抽象类,对一些基础的list操作进行封装)

    2.底层存储

    private transient Object[] elementData;
    private int size;

    object数组存储    int来计算容器的大小

    transient来修饰了 elementData  (transient关键字的作用简单说就是java自带默认机制进行序列化的时候,被其修饰的属性不需要维持)
    ArrayList采用了自定义序列化的方式
    /**
         * Save the state of the <tt>ArrayList</tt> instance to a stream (that
         * is, serialize it).
         *
         * @serialData The length of the array backing the <tt>ArrayList </tt>
         *             instance is emitted (int), followed by all of its elements
         *             (each an <tt>Object</tt> ) in the proper order.
         */
        private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException{
            // Write out element count, and any hidden stuff
            int expectedModCount = modCount ;
           s.defaultWriteObject();
    
            // Write out array length  
            s.writeInt( elementData.length );
    
            // Write out all elements in the proper order.
            for (int i=0; i<size; i++)
                s.writeObject( elementData[i]);
    
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
    
        }
    
        /**
         * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
         * deserialize it).
         */
        private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
            // Read in size, and any hidden stuff
           s.defaultReadObject();
    
            // Read in array length and allocate array
            int arrayLength = s.readInt();
            Object[] a = elementData = new Object[arrayLength];
    
            // Read in all elements in the proper order.
            for (int i=0; i<size; i++)
                a[i] = s.readObject();
        }

    序列话时,程序将数组的长度存储在了,队列开头的位置。反序列化,直接读取创建数组。

    elementData 是一个数据存储数组,而数组是定长的,它会初始化一个容量,等容量不足时再扩充容量

    比如elementData 的长度是10,而里面只保存了3个对象,那么数组中其余的7个元素(null)是没有意义的,所以也就不需要保存,以节省序列化后的内存容量

    3.构造方法

    /**
         * 构造一个具有指定容量的list
         */
        public ArrayList( int initialCapacity) {
            super();
            if (initialCapacity < 0)
                throw new IllegalArgumentException( "Illegal Capacity: " +
                                                   initialCapacity);
            this.elementData = new Object[initialCapacity];
        }
    
        /**
         * 构造一个初始容量为10的list
         */
        public ArrayList() {
            this(10);
        }
    
        /**
         * 构造一个包含指定元素的list,这些元素的是按照Collection的迭代器返回的顺序排列的
         */
        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);
        }

    默认的长度为10

    4.增加

    /**
         * 添加一个元素
         */
        public boolean add(E e) {
           // 进行扩容检查
           ensureCapacity( size + 1);  // Increments modCount
           // 将e增加至list的数据尾部,容量+1
            elementData[size ++] = e;
            return true;
        }
    
        /**
         * 在指定位置添加一个元素
         */
        public void add(int index, E element) {
            // 判断索引是否越界,这里会抛出多么熟悉的异常。。。
            if (index > size || index < 0)
               throw new IndexOutOfBoundsException(
                   "Index: "+index+", Size: " +size);
    
           // 进行扩容检查
           ensureCapacity( size+1);  // Increments modCount  
           // 对数组进行复制处理,目的就是空出index的位置插入element,并将index后的元素位移一个位置
           System. arraycopy(elementData, index, elementData, index + 1,
                          size - index);
           // 将指定的index位置赋值为element
            elementData[index] = element;
           // list容量+1
            size++;
        }
        /**
         * 增加一个集合元素
         */
        public boolean addAll(Collection<? extends E> c) {
           //将c转换为数组
           Object[] a = c.toArray();
            int numNew = a.length ;
           //扩容检查
           ensureCapacity( size + numNew);  // Increments modCount
           //将c添加至list的数据尾部
            System. arraycopy(a, 0, elementData, size, numNew);
           //更新当前容器大小
            size += numNew;
            return numNew != 0;
        }
        /**
         * 在指定位置,增加一个集合元素
         */
        public boolean addAll(int index, Collection<? extends E> c) {
            if (index > size || index < 0)
               throw new IndexOutOfBoundsException(
                   "Index: " + index + ", Size: " + size);
    
           Object[] a = c.toArray();
            int numNew = a.length ;
           ensureCapacity( size + numNew);  // Increments modCount
    
           // 计算需要移动的长度(index之后的元素个数)
            int numMoved = size - index;
           // 数组复制,空出第index到index+numNum的位置,即将数组index后的元素向右移动numNum个位置
            if (numMoved > 0)
               System. arraycopy(elementData, index, elementData, index + numNew,
                              numMoved);
    
           // 将要插入的集合元素复制到数组空出的位置中
            System. arraycopy(a, 0, elementData, index, numNew);
            size += numNew;
            return numNew != 0;
        }
    
        /**
         * 数组容量检查,不够时则进行扩容
         */
       public void ensureCapacity( int minCapacity) {
            modCount++;
           // 当前数组的长度
            int oldCapacity = elementData .length;
           // 最小需要的容量大于当前数组的长度则进行扩容
            if (minCapacity > oldCapacity) {
               Object oldData[] = elementData;
              // 新扩容的数组长度为旧容量的1.5倍+1
               int newCapacity = (oldCapacity * 3)/2 + 1;
              // 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容
               if (newCapacity < minCapacity)
                  newCapacity = minCapacity;
                // minCapacity is usually close to size, so this is a win:
                // 进行数据拷贝,Arrays.copyOf底层实现是System.arrayCopy()
                elementData = Arrays.copyOf( elementData, newCapacity);
           }
        }

    当容量大于数组长度的时候,将就容量扩大为原来的1.5倍

     5.删除:

    /**
         * 根据索引位置删除元素
         */
        public E remove( int index) {
          // 数组越界检查
           RangeCheck(index);
    
            modCount++;
          // 取出要删除位置的元素,供返回使用
           E oldValue = (E) elementData[index];
           // 计算数组要复制的数量
            int numMoved = size - index - 1;
           // 数组复制,就是将index之后的元素往前移动一个位置
            if (numMoved > 0)
               System. arraycopy(elementData, index+1, elementData, index,
                              numMoved);
           // 将数组最后一个元素置空(因为删除了一个元素,然后index后面的元素都向前移动了,所以最后一个就没用了),好让gc尽快回收
           // 不要忘了size减一
            elementData[--size ] = null; // Let gc do its work
    
            return oldValue;
        }
    
        /**
         * 根据元素内容删除,只删除匹配的第一个
         */
        public boolean remove(Object o) {
           // 对要删除的元素进行null判断
           // 对数据元素进行遍历查找,知道找到第一个要删除的元素,删除后进行返回,如果要删除的元素正好是最后一个那就惨了,时间复杂度可达O(n) 。。。
            if (o == null) {
                for (int index = 0; index < size; index++)
                  // null值要用==比较
                   if (elementData [index] == null) {
                      fastRemove(index);
                      return true;
                  }
           } else {
               for (int index = 0; index < size; index++)
                  // 非null当然是用equals比较了
                   if (o.equals(elementData [index])) {
                      fastRemove(index);
                      return true;
                  }
            }
            return false;
        }
    
        /*
         * Private remove method that skips bounds checking and does not
         * return the value removed.
         */
        private void fastRemove(int index) {
            modCount++;
           // 原理和之前的add一样,还是进行数组复制,将index后的元素向前移动一个位置,不细解释了,
            int numMoved = size - index - 1;
            if (numMoved > 0)
                System. arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size ] = null; // Let gc do its work
        }
    
        /**
         * 数组越界检查
         */
        private void RangeCheck(int index) {
            if (index >= size )
               throw new IndexOutOfBoundsException(
                   "Index: "+index+", Size: " +size);
        }

    1.数组扩容,2.数组复制,这两个操作都是极费效率的,最惨的情况下(添加到list第一个位置,删除list最后一个元素或删除list第一个索引位置的元素)时间复杂度可达O(n)。

    arraylist提供一个可以可以定义初始容量的方法,可减少数组不断地扩容不断地复制

  • 相关阅读:
    html不点击提交,自动post
    wpf slider刻度
    visual studio 的 code snippet(代码片段)
    更换手机号之前
    post提交
    动态修改settings
    获取文件数据流+叠加byte数组(给byte数组加包头包尾)
    装箱与拆箱
    ue4中窗口打开web地址
    C++通过Callback向C#传递数据,注意问题
  • 原文地址:https://www.cnblogs.com/L-a-u-r-a/p/8507431.html
Copyright © 2011-2022 走看看