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
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
解决方案 »
- 各位大哥大姐关于string字符串的处理。
- JAVA里怎么调用DLL中的函数?
- 求助:学完j2se,然后想学Hibernate,struct,spring,不知道应该先学哪个比较好?
- 静态方法能被覆盖吗?
- jsp解析xml动态生成html?
- 关于sort的迷惑(接口里的内容)
- 在线坐等大神给我解决ssh集成问题,采用注解事务,但是不能提交数据
- java多线程的线程数统计
- javescript中alert是消息框,怎样写才能在上面出现"确定""取消"两个纽,在判断是或否?
- 一个弱智问题:Color.red意思?为何类Color后直接引用,不声明一个对象,red未常数?
- InputStream为何是空指针?
- 为什么点击jbutton, jtable会有两遍数据,而且第二遍都是空值
还有你报的错误是import行,
我怀疑你其他地方有错误还有就是楼主第81行的ArrayListIterator类是额外的类?
public Iterator<AnyType> iterator() {
// TODO Auto-generated method stub
return (Iterator<AnyType>) new ArrayListIterator();
}去掉以后就通过编译了
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; }
}