zoukankan      html  css  js  c++  java
  • ArrayList

    ArrayList构造方法

    ArrayList提供了三种方式的构造器,可以构造一个默认初始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。

    // ArrayList带容量大小的构造函数。    
        public ArrayList(int initialCapacity) {    
            super();    
            if (initialCapacity < 0)    
                throw new IllegalArgumentException("Illegal Capacity: "+    
                                                   initialCapacity);    
            // 新建一个数组    
            this.elementData = new Object[initialCapacity];    
        }    
       
        // ArrayList无参构造函数。默认容量是10。    
        public ArrayList() {    
            this(10);    
        }    
       
        // 创建一个包含collection的ArrayList    
        public ArrayList(Collection<? extends E> c) {    
            elementData = c.toArray();    
            size = elementData.length;    
            if (elementData.getClass() != Object[].class)    
                elementData = Arrays.copyOf(elementData, size, Object[].class);    
        }

    元素存储

    ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)这些添加元素的方法

    // 用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素。  
    public E set(int index, E element) {  
       RangeCheck(index);  
     
       E oldValue = (E) elementData[index];  
       elementData[index] = element;  
       return oldValue;  
    }    
    // 将指定的元素添加到此列表的尾部。  
    public boolean add(E e) {  
       ensureCapacity(size + 1);   
       elementData[size++] = e;  
       return true;  
    }    
    // 将指定的元素插入此列表中的指定位置。  
    // 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。  
    public void add(int index, E element) {  
       if (index > size || index < 0)  
           throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);  
       // 如果数组长度不足,将进行扩容。  
       ensureCapacity(size+1);  // Increments modCount!!  
       // 将 elementData中从Index位置开始、长度为size-index的元素,  
       // 拷贝到从下标为index+1位置开始的新的elementData数组中。  
       // 即将当前位于该位置的元素以及所有后续元素右移一个位置。  
       System.arraycopy(elementData, index, elementData, index + 1, size - index);  
       elementData[index] = element;  
       size++;  
    }    
    // 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。  
    public boolean addAll(Collection<? extends E> c) {  
       Object[] a = c.toArray();  
       int numNew = a.length;  
       ensureCapacity(size + numNew);  // Increments modCount  
       System.arraycopy(a, 0, elementData, size, numNew);  
       size += numNew;  
       return numNew != 0;  
    }    
    // 从指定的位置开始,将指定collection中的所有元素插入到此列表中。  
    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  
     
       int numMoved = size - index;  
       if (numMoved > 0)  
           System.arraycopy(elementData, index, elementData, index + numNew, numMoved);  
     
       System.arraycopy(a, 0, elementData, index, numNew);  
       size += numNew;  
       return numNew != 0;  
       }
  • 相关阅读:
    hdu2604 矩阵快速幂
    自己对有上下界的网络流的理解
    自己对有上下界的网络流的理解
    POJ 2396 构造矩阵(上下流)
    POJ 2396 构造矩阵(上下流)
    hdu4940 有上下界的无源可行流判断
    hdu4940 有上下界的无源可行流判断
    hdu4515 小模拟
    hdu4515 小模拟
    hdu4901 枚举状态(找集合对S(xor) ==T(and))
  • 原文地址:https://www.cnblogs.com/qq1312583369/p/11118511.html
Copyright © 2011-2022 走看看