Iterator接口是否已经在java库中的某处定义(介意术语)。
即
我问的是,如果我有一个数据列表,现在我写。
Iterator itr= new Iterator();但我从来没有定义过类似的东西
public interface Iterator{ // all the methods };我是否需要导入一些已经定义了这个迭代器的包?
让我在这里举一个例子:
class BOX implements Comparable {
private double length;
private double width;
private double height;
BOX(double l, double b, double h) {
length = l;
width = b;
height = h;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea() {
return 2 * (length * width + width * height + height * length);
}
public double getVolume() {
return length * width * height;
}
public int compareTo(Object other) {
BOX b1 = (BOX) other;
if (this.getVolume() > b1.getVolume()) {
return 1;
}
if (this.getVolume() < b1.getVolume()) {
return -1;
}
return 0;
}
public String toString() {
return
“Length:
”+length +
” Width:
”+width +
” Height:
”+height;
}
} // End of BOX class这是我的测试课。
import java.util.*;
class ComparableTest {
public static void main(String[] args) {
ArrayList box = new ArrayList();
box.add(new BOX(10, 8, 6));
box.add(new BOX(5, 10, 5));
box.add(new BOX(8, 8, 8));
box.add(new BOX(10, 20, 30));
box.add(new BOX(1, 2, 3));
Collections.sort(box);
Iterator itr = ar.iterator();
while (itr.hasNext()) {
BOX b = (BOX) itr.next();
System.out.println(b);
}
}
}// End of class现在在类ComparableTest中也不应该实现interface iterator,我是不是应该定义一个包含所有方法的interface iterator。另外,迭代器方法的实现在哪里?
我可能很困惑,但请帮忙!
谢谢。