我正在为自己的数组列表类编写迭代器方法,但是,当我尝试测试该类时,它说foreach循环不适用于MyArrayList。谁能帮助我解决我出错的地方?
类本身使用对象数组和必要的方法来像arraylist(添加,删除,获取等)。
这是类构造函数和我的迭代器类:
public class MyArrayList {
public Object[] arrayList = new Object[5];
int length1 = 5;
public MyArrayList() {
}
public MyArrayList(Object[] arrayList) {
this.arrayList = arrayList;
length1 = arrayList.length;
}
public ArrayListIterator iterator(){
return new ArrayListIterator(this);
}
class ArrayListIterator<MyArrayList> implements Iterator<Object> {
private Object[] arrayListIterable;
private int count = 0;
public ArrayListIterator(Object[] x){
arrayListIterable = x;
}
public ArrayListIterator(MyArrayList myArrayList) {
}
public boolean hasNext(){
if(count < arrayList.length){
return true;
}else{
return false;
}
}
public Object next(){
int x = count;
count++;
return arrayListIterable[x];
}
}
答案 0 :(得分:3)