ArrayList类的实现

来源:互联网 发布:如何利用网络找客户 编辑:程序博客网 时间:2024/05/16 12:49
Code:
  1. import java.util.Iterator;   
  2. import java.util.NoSuchElementException;   
  3.   
  4. public class MyArrayList<AnyType> implements Iterable<AnyType> {   
  5.     private static final int DEFAUTL_CAPACITY = 10;   
  6.   
  7.     private int theSize;   
  8.     private AnyType[] theItems;   
  9.   
  10.     public MyArrayList() {   
  11.         clear();   
  12.     }   
  13.   
  14.     public void clear() {   
  15.         theSize = 0;   
  16.         ensureCapacity(DEFAUTL_CAPACITY);   
  17.     }   
  18.   
  19.     public int size() {   
  20.         return theSize;   
  21.     }   
  22.   
  23.     public boolean isEmpty() {   
  24.         return size() == 0;   
  25.     }   
  26.   
  27.     public void trimToSize() {   
  28.         ensureCapacity(size());   
  29.     }   
  30.   
  31.     public AnyType get(int idx) {   
  32.         if (idx < 0 || idx >= size())   
  33.             throw new ArrayIndexOutOfBoundsException();   
  34.         return theItems[idx];   
  35.     }   
  36.   
  37.     public AnyType set(int idx, AnyType newVal) {   
  38.         if (idx < 0 || idx > size())   
  39.             throw new ArrayIndexOutOfBoundsException();   
  40.         AnyType old = theItems[idx];   
  41.         theItems[idx] = newVal;   
  42.         return old;   
  43.     }   
  44.   
  45.     public void ensureCapacity(int newCapacity) {   
  46.         if (newCapacity < theSize)   
  47.             return;   
  48.         // 为数组分配内存,并将旧内容拷贝到新数组中   
  49.         AnyType[] old = theItems;   
  50.         // 因为创建泛型数组是非法的,所以创建一个泛型类型界限的数组,然后使用一个数组进行类型转换   
  51.         // 这将产生一个编译器警告,但在泛型集合的实现中这是不可避免的   
  52.         // 可用注解@SuppressWarnings("unchecked")解除警告   
  53.         theItems = (AnyType[]) new Object[newCapacity];   
  54.         for (int i = 0; i < size(); i++)   
  55.             theItems[i] = old[i];   
  56.     }   
  57.   
  58.     // 添加到表的末端病通过调用添加到指定位置的较一般的版本而得以简单的实现   
  59.     public boolean add(AnyType x) {   
  60.         add(size(), x);   
  61.         return true;   
  62.     }   
  63.   
  64.     /*  
  65.      * 计算上来说是昂贵的,因为它需要移动在指定位置上或指定位置后面的那些元素到一个更高的位置上  
  66.      */  
  67.     public void add(int idx, AnyType x) {   
  68.         // add方法可能要求增加容量,扩充容量的代价也是很大的   
  69.         if (theItems.length == size())   
  70.             ensureCapacity(size() * 2 + 1);   
  71.         for (int i = theSize; i > idx; i--)   
  72.             theItems[i] = theItems[i - 1];   
  73.         theItems[idx] = x;   
  74.   
  75.         theSize++;   
  76.     }   
  77.   
  78.     // remove方法类似于add,只是由高位置向低位置移动   
  79.     public AnyType remove(int idx) {   
  80.         AnyType removedItem = theItems[idx];   
  81.         for (int i = idx; i < size() - 1; i++)   
  82.             theItems[i] = theItems[i + 1];   
  83.   
  84.         theSize--;   
  85.         return removedItem;   
  86.     }   
  87.   
  88.     @Override  
  89.     public Iterator<AnyType> iterator() {   
  90.         return new ArrayListIterator();   
  91.     }   
  92.   
  93.     private class ArrayListIterator implements Iterator<AnyType> {   
  94.         private int current = 0;   
  95.   
  96.         @Override  
  97.         public boolean hasNext() {   
  98.             return current < size();   
  99.         }   
  100.   
  101.         @Override  
  102.         public AnyType next() {   
  103.             if (!hasNext())   
  104.                 throw new NoSuchElementException();   
  105.             return theItems[current++];   
  106.         }   
  107.   
  108.         @Override  
  109.         public void remove() {   
  110.             MyArrayList.this.remove(--current);   
  111.         }   
  112.     }   
  113. }   

 

原创粉丝点击