java跳回循环某处_接口迭代器已经在Java中的某处定义了?

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。另外,迭代器方法的实现在哪里?

我可能很困惑,但请帮忙!

谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值