【发布时间】:2016-01-23 10:54:18
【问题描述】:
我的课程有问题,我无法解决。我想为我的Interval1 类实现一个迭代器。这是我的代码:
public class Interval1 {
private double first;
private double last;
private double step;
private IntervalIterator e;
public Interval1(double first, double last, double step, IntervalIterator e) {
//chequear los datos
this.first = first;
this.last = last;
this.step = step;
this.e = new IntervalIterator();
}
public double at(int index) {
checkIndex(index);
return first + index*step;
}
private void checkIndex(int index) {
if(index < 0 || index >= size())
throw new IndexOutOfBoundsException("Invalid Index: " + index);
}
public int size() {
return (int) ((last - first) / step);
}
public IntervalIterator e() {
for (Double i : this)
System.out.println(i);
return e;
}
}
这是我正在使用的 IntervalIterator 类,这会在 size() 和 at 中给出错误:
public class IntervalIterator implements Iterator<Double> {
private int index = 0;
private Double at;
public boolean hasNext() {
return index < size();
}
public Double next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return at(index++);
}
public void remove() {
if (Interval1 <= 0) {
return throw new UnsupportedOperationException("remove");
} else {
if (Interval1 >= 0) {
//chekear el array e eliminarlo
}
}
}
}
【问题讨论】:
-
开始格式化您的代码,使其可读。
-
在
hasNext(),您希望代码调用哪个size()方法?字段at用于什么?在next()中,您希望该代码调用哪个at()方法?在remove()中,变量/字段/参数Interval1定义在哪里? -
在
Interval1构造函数中,参数e是干什么用的?一个迭代器只能使用一次,所以第二次调用e()(奇怪的名字)将返回已经用尽的迭代器。在e()中,this不是一个可迭代对象,for不能在for这样的循环中使用。 -
对于第一个/最后一个/步骤值 3/9/2 我希望值
3,5,7,9返回,但size()返回 @ 987654345@,不是4。