인터페이스는 Iterator
이 자바 라이브러리의 어딘가에 이미 정의되어 있습니다 (전문 용어를 염두에 두십시오). 즉인터페이스 반복기는 이미 Java 어딘가에 정의되어 있습니까?
내가 묻는 것은 내가 arraylist가 있다고 말하면서, 이제는 씁니다.
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