如何在Java中将迭代器实现为类的属性 [英] How to implement iterator as an attribute of a class in Java
问题描述
假设我有这个简单的MyArray类,有两个简单的方法:add,delete和iterator。在main方法中,我们可以看到它应该如何使用:
let's say I have this simple MyArray class, with two simple methods: add, delete and an iterator. In the main method we can see how it is supposed to be used:
public class MyArray {
int start;
int end;
int[] arr;
myIterator it;
public MyArray(){
this.start=0;
this.end=0;
this.arr=new int[500];
it=new myIterator();
}
public void add(int el){
this.arr[this.end]=el;
this.end++;
}
public void delete(){
this.arr[this.start]=0;
this.start++;
}
public static void main(String[] args){
MyArray m=new MyArray();
m.add(3);
m.add(299);
m.add(19);
m.add(27);
while(m.it.hasNext()){
System.out.println(m.it.next());
}
}
然后以某种方式实现MyIterator:
And then MyIterator should be implemented somehow:
import java.util.Iterator;
public class myIterator implements Iterator{
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
MyIterator应迭代 arr 来自 MyArray 类,从开始到 end 值;两者都是 MyArray 的属性。因此,由于 MyIterator 应该使用 MyArray 属性,MyIterator应该如何实现?也许我可以在初始化中发送当前对象:
MyIterator should iterate arr from MyArray class, from start to end values; both are also attributes of MyArray. So, as MyIterator should use MyArray attributes, how should MyIterator be implemented? Perhaps I can send the current object in the initialization:
it=new myIterator(this);
但我想这不是最好的灵魂。或者也许MyArray本身应该实现Iterator接口?这是怎么解决的?
But I guess it's not the best soultion. Or maybe MyArray itself should implement Iterator interface? How is this solved?
编辑:
好的,谢谢大家。这是我想要做的一个简单的例子,所以不关心固定长度数组。我真的想做的是循环FIFO,这就是为什么 start
和 end
是游标。
Ok, thanks to everybody. This was a simple example of what I wnat to do, so don't care about fixed length array. Waht I really want to do is a circular FIFO, that's why start
and end
are the cursors.
这个循环FIFO将是一对整数的数组,例如,大小为300: int [] [] arr = new int [300] [2]
。
This circular FIFO will be an array of pairs of ints with, e.g., size 300: int[][] arr=new int[300][2]
.
当迭代一个圆形数组时,如果计数器到达终点并让它从头开始,我必须小心,所以这个是我如何解决它:
When iterating a circular array I have to take care if the counter arrives to the end and make it start from the beginning, so this is how I have solved it:
if (this.start >= this.end ) temp_end=this.end+this.buff.length;
else temp_end=this.end;
int ii;
int j=0;
int[] value=new int[2];
for(int i=this.start; i<temp_end; i++){
ii=i% this.arr.length;
value=this.buff[ii];
//do anything with value
}
但是我想避免担心这些事情并且只是以一种简单的方式迭代,我可以用迭代器接口来做,但是我有2个问题:第一个我已经解释过并且已经被很多人解决了答案,第二个是我的数组由一对int组成,我不能使用原始类型的迭代器。
But I would like to avoid worrying about these things and just iterate in a simple way, I can do this with iterator interface, but then I have 2 problems: the first one I already explained and has been solved by many answers, and the second one is that my array is made of pairs of ints, and I can't use iterator with primitive types.
推荐答案
将迭代器维护为类的实例变量非常不寻常。你只能遍历一次数组 - 可能不是你想要的。更有可能的是,您希望您的类为想要遍历数组的任何人提供迭代器。下面是一个更传统的迭代器。
Its very unusual to maintain an iterator as an instance variable of the class. You can only traverse the array once - probably not what you want. More likely, you want your class to provide an iterator to anyone that wants to traverse your array. A more traditional iterator is below.
Java 5+代码 - 我没有尝试编译或运行,因此可能包含错误(不在dev机器右侧附近)现在)。它还使用autobox'ing将 Integer
转换为 int
。
Java 5+ code - I haven't tried to compile or run, so it may be contain errors (not near a dev machine right now). It also uses autobox'ing for converting Integer
to int
.
public class MyArray implements Iterable<Integer> {
public static class MyIterator implements Iterator<Integer> {
private final MyArray myArray;
private int current;
MyIterator(MyArray myArray) {
this.myArray = myArray;
this.current = myArray.start;
}
@Override
public boolean hasNext() {
return current < myArray.end;
}
@Override
public Integer next() {
if (! hasNext()) throw new NoSuchElementException();
return myArray.arr[current++];
}
@Override
public void remove() {
// Choose exception or implementation:
throw new OperationNotSupportedException();
// or
//// if (! hasNext()) throw new NoSuchElementException();
//// if (currrent + 1 < myArray.end) {
//// System.arraycopy(myArray.arr, current+1, myArray.arr, current, myArray.end - current-1);
//// }
//// myArray.end--;
}
}
....
// Most of the rest of MyArray is the same except adding a new iterator method ....
public Iterator<Integer> iterator() {
return new MyIterator();
}
// The rest of MyArray is the same ....
}
另请注意:请注意不要在静态阵列上达到500元素限制。如果可以,请考虑使用ArrayList类。
Also note: be careful of not hitting that 500 element limit on your static array. Consider using the ArrayList class instead if you can.
这篇关于如何在Java中将迭代器实现为类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!