【发布时间】:2012-03-13 05:25:58
【问题描述】:
我可以编写一个嵌套循环来遍历嵌套数组的元素,for-each 优雅地隐藏了遍历嵌套数组每一层的细节:
Foo[][] dbl_array;
public void do_all() {
// Iterate over both levels of a nested array, invoking "bar" on each inner element.
for (final Foo[] arr_1d : dbl_array) {
for (final Foo el : arr_1d) {
el.bar();
}
}
}
但是这种方法的问题是:
- 需要使用双重嵌套循环来遍历数据结构这一事实在这里非常明显。
- 我必须为需要调用内部元素的每个函数复制这个嵌套循环。
- 这打破了遍历结构的方法的封装。我可能会选择使用其他结构来实现嵌套数组,并且不想将嵌套迭代的每个副本都更改为所需的任何遍历方法。
- 嵌套的 for-each 循环的结构是由内而外的。 Iterator 应该在内部处理数据结构遍历,而不是在嵌套内部调用所需的函数,从而暴露遍历过程中遇到的每个条目。
那么...我该如何更改它以便实现一个可以调用的迭代器:
Foo_Iterator fi = Foo.iterator();
for (final Foo el : fi) { // The Iterator hides the traversal details from the caller.
el.bar(); // The desired function is invoked on each element encountered.
}
这会将迭代如何完成的细节留给 Foo_Iterator 类。
我的问题是“我如何编写 Foo_Iterator,跟踪嵌套迭代器的状态? 我认为它看起来像下面这样,但我错过了跟踪状态的位。
class Foo_Iterator extends Whiz implements Iterator {
public Foo_Iterator() {
// Initialize state based on access to the superclass Whiz.
}
public boolean hasNext() {
// Is there an elegant way to save the state of both iterators between each call to hasNext() and next()?
// The "inelegant" way would be to keep track of the inner and out array indices,
// comparing the current index to the array length...
}
public Foo next() {
// Access the "next" in the nested sequence.
}
public void remove() {
// I probably won't implement or need/use this one.
}
}
关于如何以“优雅”的方式做到这一点有什么建议吗?
谢谢。
【问题讨论】:
标签: java foreach iterator nested-loops