import java.util.Iterator;
public class MyArrayList<AnyType> implements Iterable<AnyType>{
private static final int DEFAULT_CAPACITY = 10;

private int theSize ; //数组大小
private AnyType[] theItems; //数组

public MyArrayList(){ //构造方法
clear() ;
}

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] ;
}
} private int size() { //元素数
return theSize ; 
}

public void clear(){
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}

public boolean isEmpty(){ //测试列表是否为空
return size() == 0 ;
}

public void trimToSize(){ //将实例容量调整为列表当前大小
ensureCapacity( size() );
}

public AnyType get(int index){ //返回列表中指定的元素
if(index < 0 || index >= size())
throw new ArrayIndexOutOfBoundsException();
return theItems[index] ; 
}

public AnyType set(int index , AnyType newVal){ //替代
if(index < 0 || index >= size())
throw new ArrayIndexOutOfBoundsException();
AnyType old = theItems[index] ;
theItems[index] = newVal ;
return old ; //返回位于该指定位置上的元素

}

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

public boolean add(AnyType x){
add(size() ,  x);
return true;
}

public AnyType remove(int index){
AnyType removedItem = theItems[index] ;
for(int i = index ; i < size() ; i ++)
theItems[i] = theItems[i + 1];
theSize --;
return removedItem ;
} @Override
public Iterator<AnyType> iterator() {
// TODO Auto-generated method stub
return (Iterator<AnyType>) new ArrayListIterator();
}
}下面编译器都没抱错  ,但是第一句import java.util.Iterator;编译器报了一个错误The type AnyType cannot be resolved. It is indirectly referenced from required .class files
想问一下各位大神。这是怎么回事?java  AnyType

解决方案 »

  1.   

    AnyType应该没问题啊
    还有你报的错误是import行,
    我怀疑你其他地方有错误还有就是楼主第81行的ArrayListIterator类是额外的类?
      

  2.   

    AnyType木有问题,我把你最后这句 @Override
        public Iterator<AnyType> iterator() {
            // TODO Auto-generated method stub
            return (Iterator<AnyType>) new ArrayListIterator();
        }去掉以后就通过编译了
      

  3.   

    需要自己实现一个ArrayListIterator    @Override
        public Iterator<AnyType> iterator() {
            // TODO Auto-generated method stub
            return (Iterator<AnyType>) new ArrayListIterator<AnyType>();
        }
        
        // 仅仅为了通过编译,没有其他用途
        private static class ArrayListIterator<AnyType> implements Iterator<AnyType> {
            public void remove() {}
            public AnyType next() {return null;}
            public boolean hasNext() { return false; }
        }