ArayListクラスの手動作成

2263 ワード

import java.util.Iterator;


public class MyArrayList implements Iterable{

	
	private static final int DEFAULT_CAPACITY=10;
	
	private int theSize;//    
	private AnyType[] theItems;//    
	
	public MyArrayList(){
		doClear();
	}
	
	public void clear(){
		doClear();
	}	
	private void doClear(){//     
		theSize=0;
		ensureCapacity(DEFAULT_CAPACITY);
	}
	
	public int size(){//      
		return theSize;
	}
	
	public boolean isEmpty(){
		return size()==0;
	}
	
	public void trimToSize(){
		ensureCapacity(size());
	}
	
	public AnyType get(int idx){//     
		if(idx<0||idx>=size()){
			throw new ArrayIndexOutOfBoundsException();
			
		}
		return theItems[idx];
	}
	
	public AnyType set(int idx,AnyType newVal){//        
		if(idx<0||idx>=size()){
			throw new ArrayIndexOutOfBoundsException();
		}
		AnyType old = theItems[idx];
		theItems[idx]=newVal;
		return old;
	}
	
	
	@SuppressWarnings("unchecked")//         
	public void ensureCapacity(int newCapacity){//               
		if(newCapacityidx;i--){
			theItems[i]=theItems[i-1];
		}
		theItems[idx]=x;
		theSize++;
	}
	
	public AnyType remove(int idx){
		AnyType removedItem  = theItems[idx];
		for(int i=idx;i iterator() {//   
		// TODO Auto-generated method stub
		return new ArrayListIterator(this);
	}
	
	//   
	private class ArrayListIterator implements java.util.Iterator
	{
		private int current=0;
		private MyArrayList theList;
	
		public ArrayListIterator(MyArrayList list){
			this.theList=list;
		}
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return current