package sjjg;
import java.util.Iterator;class MyArrayList<AnyType> implements Iterable <AnyType>{
private static final int DEFAULT_CAPACITY=10;
    private int theSize;
    private AnyType[] theItems;
    MyArrayList(){
     clear();
    }
    public void clear(){
     theSize=0;
     ensureCapacity(DEFAULT_CAPACITY);
    
    }
    
    public int size(){
return theSize;
    }
    public boolean isEmpty(){
     return theSize==0;
    }
    public void trimToSize(){
     ensureCapacity(size());
    }
    public AnyType get(int idx){
     if(idx<0||idx>=theSize)
     throw new ArrayIndexOutOfBoundsException();
return theItems[idx];
    
    }
    public AnyType set(int idx,AnyType newVal){
     if(idx<0||idx>=theSize)
     throw new ArrayIndexOutOfBoundsException("Index"+idx+";size"+theSize);
     AnyType oldItems=theItems[idx];
     theItems[idx]=newVal;
return oldItems;
    
    }
@SuppressWarnings("unchecked")
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];

}
public void add(AnyType x){
add(size(),x);
}
public void add(int idx,AnyType x){
if(theItems.length==size()){
ensureCapacity(size()*2+1);}
for(int i=theSize;i>idx;i--){
theItems[i]=theItems[i-1];
theItems[idx]=x;

theSize++;//增加元素后,容量也要增加
}

}
public AnyType remove(int idx){
AnyType removeIdex=theItems[idx];
for(int i=idx;i<size()-1;i++)
theItems[i]=theItems[i+1];

theSize--;
return removeIdex;
}
public java.util.Iterator<AnyType> iterator(){
return new ArrayListIterator();

}
public class ArrayListIterator  implements Iterator<AnyType> {
private int current=0;

public boolean hasNext(){
return current<size();
}

public AnyType next(){
if(!hasNext())
throw new java.util.NoSuchElementException();
return theItems[current++];
} public void remove() {
MyArrayList.this.remove(--current);

}
}
}public class TestMyArrayList {
public static void main(String[] args){
MyArrayList<Integer> myl=new MyArrayList<Integer>();
myl.add(2);
myl.add(4);
myl.add(6);
myl.add(8);
myl.add(10);

System.out.println("原数据:");
for(int i=0;i<myl.size();i++){
System.out.println(myl.get(i));
}

myl.add(2,3);
System.out.println();
System.out.println("在第三位置增加数据后:");
for(int i=0;i<myl.size();i++){
System.out.print(myl.get(i)+"    ");
}

myl.remove(1);
System.out.println();
System.out.println("删除第二个数据后:");
for(int i=0;i<myl.size();i++){
System.out.print(myl.get(i)+"   ");
}







}}

解决方案 »

  1.   

    add(int idx, AnyType x)问题出在这个方法中了,
    因为你在main方法中创建MyArrayList时 要调用ensureCapacity方法,但是你传进去的值是10
     MyArrayList()
    {
    clear();
    } public void clear()
    {
    theSize = 0;
    ensureCapacity(DEFAULT_CAPACITY); }
    而在ensureCapacity方法中你将一个大小为10的数组赋值给theItems 
    theItems = (AnyType[]) new Object[newCapacity];
    这导致了你在调用add方法时下面的代码是不会执行的:
    if (theItems.length == size())
    {
    ensureCapacity(size() * 2 + 1);
    }
    所以在执行这个for循环的时候你根本不能把元素添加进去
    for (int i = theSize; i > idx; i--)
    {
    theItems[i] = theItems[i - 1];
    theItems[idx] = x; theSize++;// 增加元素后,容量也要增加
    }
      

  2.   

    那我应该怎么那我应该怎么改呢?请说的详细点,我刚刚学java,万分感谢