链表之ArrayList

来源:互联网 发布:扬州大学怎么样 知乎 编辑:程序博客网 时间:2024/05/16 01:27

链表是最经常用的数据结构之一,可以是栈和队列的实现的基础。下面直接上代码:

a)modCount 在AbstractList中定义。用来表示链表改变的次数。

b)注意 RangeCheck函数。

c)这里没提及Iterator类,其中iterator类在AbtractList中有一个内部类Itr来表示,用iterator()来引入

    public Iterator<E> iterator() {
        return new Itr();
    }

 

 

 

 

-----------------------------------------------------------------------------------------------------------------------

package cai.util;
import java.util.*;
public class MyArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, java.io.Serializable{
    /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer.
     */
    private transient E[] elementData;
    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;
    //public int modCount;  modCount is defined in AbstractList


    public MyArrayList(int initialCapacity){
        super();
        if(initialCapacity < 0){
            throw new IllegalArgumentException("Illegal Capacity :" + initialCapacity);
        } 
        elementData = (E[]) new Object[initialCapacity]; 
    }


   public MyArrayList(){
       this(10); 
   }


   public void trimToSize(){
       modCount ++;
       int oldCapacity = elementData.length;
       if(size < oldCapacity){
           elementData = Arrays.copyOf(elementData, size); 
       } 
   }


   public void ensureCapacity(int minCapacity){
       modCount++;
       int oldCapacity = elementData.length;
       if(minCapacity > oldCapacity){
           elementData = Arrays.copyOf(elementData, minCapacity);
       } 
   }
   /**
     * Returns the number of elements in this list.
     *
     * @return the number of elements in this list
     */
   public int size(){
       return size; 
   }
   /**
     * Returns <tt>true</tt> if this list contains no elements.
     *
     * @return <tt>true</tt> if this list contains no elements
     */
   public boolean isEmpty(){
       return size == 0; 
   }


   public boolean contains(Object o) {
       return indexOf(o) >= 0;
    }
    public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
     if (elementData[i]==null)
         return i;
    } else {
        for (int i = 0; i < size; i++)
     if (o.equals(elementData[i]))
         return i;
    }
    return -1;
    }


    public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }   


   public E get(int index) {
    RangeCheck(index);
    return (E) elementData[index];
    }


   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);  // Increments modCount!!
    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!!
    System.arraycopy(elementData, index, elementData, index + 1,
       size - index);
    elementData[index] = element;
    size++;
    }
    public E remove(int index) {
    RangeCheck(index);   
    modCount++;
    E oldValue = (E) elementData[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   
    return oldValue;
    }


    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // Let gc do its work
    }
    public boolean remove(Object o) {
    if (o == null) {
       for (int index = 0; index < size; index++)
         if (elementData[index] == null) {
             fastRemove(index);
             return true;
         }
    } else {
        for (int index = 0; index < size; index++)
      if (o.equals(elementData[index])) {
          fastRemove(index);
          return true;
      }
     }
      return false;
    }   


    public void clear() {
         modCount++;
         // Let gc do its work
         for (int i = 0; i < size; i++)
              elementData[i] = null; 
         size = 0;
    }


    private void RangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
    }


    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();
        }

    }


    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];
        for (int i=0; i<size; i++)
             a[i] = s.readObject();
       }

}

原创粉丝点击