【发布时间】:2015-10-02 23:19:36
【问题描述】:
我是 Java 中整个“迭代器”概念的新手,需要帮助在我的代码中实现它。代码如下:
class IteratorExample {
int tstArray [];
IteratorExample(){
}
public void addNum(int num){
tstArray[0] = num; //And yes, I can only add one number at the moment, but it
is not what I want to focus on right now.
}
public Iterator<Integer> innerIterator(){
return new methodIterator();
}
class methodIterator implements Iterator<Integer> {
public int index;
private methodIterator(){
index = 0;
}
public boolean hasNext(){
return index < tstArray.length;
}
public Integer next(){
return;
}
}
public static void main(String[] args){
IteratorExample sample = new IteratorExample();
test(sample);
}
public static void test(IteratorExample arr){
arr.addNum(1);
system.out.print(arr);
}
}
这是到目前为止编写的代码。我想这样做,所以我可以使用 addNum() 方法向数组添加一个数字,然后使用 system.print 从 main 显示它(是的,我知道我需要一个 toString 方法才能获得数字up 而不是内存地址,稍后会实现,现在我只专注于让它工作。)
【问题讨论】:
-
您的实际问题是什么?
-
“是的,我现在只能加一个数字,但这不是我现在想要关注的。” - 实际上,这是你的应该专注于。 '因为它包括迭代器需要的基础设施。
-
为什么要向非集合类型添加迭代器?
-
我正在尝试将数组和数组列表与迭代器之间的运行速度进行比较。这是非常规的(我可以补充一点不实用吗?)但这是真正的原因。
-
你不能打印内存地址btw