1. 程式人生 > >ArrayList類的實現,利用陣列儲存元素

ArrayList類的實現,利用陣列儲存元素

避免與類庫中的ArrayList混淆,我將我實現的類命名為MyArrayList

基本思想:在MyArrayList內部用一個數組來儲存插入元素,元素滿則擴充陣列容量。

實現細節如下:

1、儲存基礎陣列,陣列的容量,以及儲存在MyArrayList中的當前項;

2、提供一種可以改變陣列容量的機制,每當要插入元素且檢查到當前儲存元素的陣列滿時,便建立一個兩倍容量的新陣列,將老陣列的元素複製到新陣列,並將插入元素也插入新陣列,回收老陣列(垃圾回收機制)。

3、提供get和set的實現;

4、提供如size()/isEmpty()/clear()/remove/add()等方法;

5、提供一個實現Iterator的類,這個類儲存迭代序列中的下一項的下標,提供next()/hasNext()/remove()等方法的實現。

實現程式碼如下:

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @description  ArrayList的實現 
 * @author Zhangx
 * @date 2015年12月1日 下午12:34:39
 * @version 1.0 
 */
public class MyArrayList<AnyType> implements Iterable<AnyType>{
	
	private static final int DEFAULT_CAPACITY = 10;   //預設初始大小
	
	private int theSize;              //當前元素個數
	private AnyType[] theItems;       //MyArrayList內部用來儲存元素的陣列
	
	public MyArrayList(){             //呼叫clear(),初始化List
		clear();
	}
	
	/**
	 * 將元素個數設定為0,並初始化一個大小為10的陣列儲存元素
	 */
	public void clear(){
		theSize = 0;
		ensureCapacity(DEFAULT_CAPACITY);
	}
	
	/**
	 * 獲得list元素個數
	 * @return int
	 */
	public int size(){
		return theSize;
	}
	
	/**
	 * list是否為空
	 * @return boolean(true|false)
	 */
	public boolean isEmpty(){
		return size()==0;
	}
	
	/**
	 * 將list元素放入一個大小為元素個數的新陣列
	 */
	public void trimToSize(){
		ensureCapacity(size());
	}
	
	/**
	 * 獲得指定位置元素,如果索引超出陣列範圍則丟擲異常
	 * @param idx
	 * @return AnyType
	 */
	public AnyType get(int idx){
		if(idx<0||idx>=size())
			throw new ArrayIndexOutOfBoundsException();
		
		return theItems[idx];
	}
	
	/**
	 * 在指定位置設定元素,如果索引超出陣列範圍則丟擲異常
	 * @param idx
	 * @param newVal
	 * @return AnyType
	 */
	public AnyType set(int idx, AnyType newVal){
		if(idx<0||idx>=size()){
			throw new ArrayIndexOutOfBoundsException();
			
		AnyType old = theItems[idx];
		theItems[idx] = newVal;
		
		return old;
		}
	}
	
	/**
	 * 根據傳入的大小,獲得一個新陣列,並將元素儲存新陣列
	 * @param newCapacity
	 */
	public void ensureCapacity(int newCapacity){
		if(newCapacity<theSize){
			return;
		}
		
		AnyType[] old = theItems;
		theItems = (AnyType[]) new Object[newCapacity];
		for(int i=0;i<size();i++){
			theItems[i] = old[i];
		}
	}
	
	/**
	 * 新增元素
	 * @param x
	 * @return boolean
	 */
	public boolean add(AnyType x){
		add(size(),x);
		return true;
	}
	
	/**
	 * 在指定位置增加元素,如果當前陣列已滿,則建立一個兩倍大小的新陣列,然後複製元素,插入元素
	 * @param idx
	 * @param x
	 */
	public void add(int idx, AnyType x){
		if(theItems.length==size()){
			ensureCapacity(size()*2);
		}
		
		for(int i=theSize;i>idx;i--){
			theItems[i] = theItems[i-1];
		}
		
		theItems[idx] = x;
		
		theSize++;
	}
	
	/**
	 * 刪除指定位置元素,並將刪除元素以後的元素前移一個位置
	 * @param idx
	 * @return
	 */
	public AnyType remove(int idx){
		AnyType removedItem = theItems[idx];
		for(int i=idx;i<size()-1;i++){
			theItems[i] = theItems[i+1];
		}
		
		theSize--;
		
		return removedItem;
	}
	
	@Override
	public Iterator<AnyType> iterator() {
		return new ArrayListIterator();
	}

	/**
	 * 迭代器
	 * @author Zhangxin
	 *
	 */
	private class ArrayListIterator implements Iterator<AnyType>{
		
		private int current = 0;    //記錄一個位置的索引

		/**
		 * 判斷索引當前位置的下一個位置是否還有元素
		 */
		@Override
		public boolean hasNext() {
			return current<size();
		}

		/**
		 * 返回索引當前位置的元素,並指向下一個元素
		 */
		@Override
		public AnyType next() {
			if(!hasNext()){
				throw new NoSuchElementException();
			}
			return theItems[current++];
		}

		/**
		 * 刪除索引前一個位置的元素
		 */
		@Override
		public void remove() {
			MyArrayList.this.remove(--current);			
		}		
	}
	
}