自定義類,實現ArrayList基本功能
阿新 • • 發佈:2019-01-10
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class CreatArrayList<E> {
/** 資料將被儲存在裡面*/
private transient Object[] elementData;
/**CreatArrayList裡面的元素的個數*/
private int size;
/**
* CreatArrayList的最大的容器的大小
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
*這個就用多執行緒的判斷
*/
protected transient int modCount = 0;
/**
* 構造一個空的且初始化其大小的 CreatArrayList
*/
public CreatArrayList(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* 預設的CreatArrayList的初始化大小
*/
public CreatArrayList() {
this(10);
}
/**
* 增加一個元素在CreatArrayList 的最末尾
*/
public boolean add(E e) {
ensureCapacity(size + 1); //第一步首先判斷是否要擴容,如果擴容就會進一步操作
elementData[size++] = e;
return true;
}
/**
* 檢查是否要擴容
* 判斷標準:增加一個元素後的大小minCapacity(size+1)
* 其實就是增加一個元素後,元素的個數是是否超過容器的大小
*
*/
private void ensureCapacity(int minCapacity){
if (minCapacity - elementData.length > 0)
growCapacity(minCapacity);
}
/**
* 對CreatArrayList進行擴容,傳入的引數是元素的個數,其實就是容器最小的個數
*/
private void growCapacity(int minCapacity){
int oldCapacity=elementData.length;
int newCapacity=oldCapacity+(oldCapacity>>1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)//如果已下子擴的太大,那麼就等於
newCapacity = hugeCapacity(minCapacity);
//把舊的容器裡面的資料複製到新的容器大小為(newCapacity)裡面,並且返回新的資料
elementData = Arrays.copyOf(elementData, newCapacity);
}
/**
* 這個其實也就是當newCapacity>大於MAX_ARRAY_SIZE的時候
* 那麼就會判斷資料的大小minCapacity
* 如果minCapacity>MAX_ARRAY_SIZE 那麼只增加8個大小,也就是Integer.MAX_VALUE
* minCapacity<MAX_ARRAY_SIZE,那麼容器的大小就變成MAX_ARRAY_SIZE
*/
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
* 刪除指定位置的元素
*/
@SuppressWarnings("unchecked")
public E remove(int index) {
rangeCheck(index);
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 rangeCheck(int index) {
if (index >= size)
new Exception("陣列越界");
}
/**
* 建立一個基本的內部類用於進行迭代
*/
public Iterator<E> iterator() {
return new CreatItr();
}
/**
* 內部類
* */
private class CreatItr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor!=size;
}
@SuppressWarnings("unchecked")
public E next() {
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = CreatArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
/**
* 判斷執行緒是否出問題
* 這個就是為什麼在迭代過程中間刪除元素會丟擲異常的原因
*/
@SuppressWarnings("unused")
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
}
簡單 測試自己寫的集合
import java.util.Iterator;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
CreatArrayList creatList=new CreatArrayList();
creatList.add("1");
creatList.add("2");
creatList.add("3");
creatList.add("4");
creatList.add("5");
Iterator tir=creatList.iterator();
while(tir.hasNext()){
System.out.print(tir.next());
}
}
}