【发布时间】:2014-11-17 19:22:29
【问题描述】:
我试图通过一些示例来了解 Java 中的迭代器功能,但我不知道如何访问迭代器以便开始打印集合。
所以我有这个功能:
class A <Item> implements Iterable <Item> {
//....does stuff
}
public Iterator <Item> iterator() {
return new Itor();
}
class Itor implements Iterator <Item> {
//.....custom Iterator functions within
}
基本上,类 A 是一个包含值列表的数据结构,我想使用另一个类中的迭代器将这些值打印出来。我将如何实施?我不确定语法会是什么样子。
假设我还有一门课
class B {
private class Node {
A<Integer> n; // I have another class that inserts values into the A class
// and I want to be able to print n out.
}
private void printA() {
Iterator it = A.iterator(); //I get the non-static method error here.
}
}
【问题讨论】:
-
错误:
When I try to declare the Iterator, I get Non-static method iterator() cannot be referenced from a static context.请告诉我们导致错误的代码。我猜你可能在main()中做错了什么。
标签: java printing iterator iterable