接口Iterator
是否已經在java庫中的某處定義(介意術語)。 即接口迭代器已經在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
將包含所有的方法。另外,迭代器方法的實現在哪裏?
我也許很困惑,但請幫忙! 謝謝。
java.lang.Iterator的方法由ArrayList類實現。 – hellectronic