如何实现嵌套列表迭代器
在一次采访中,我被问到这个问题:
“如何实现自定义嵌套列表迭代器?”
方法next()
应首先返回每个列表的第一个元素,然后再返回第二个元素,等等。
输入示例:
[[1,2,3],[4,5],[6],[],[7,8,9]]
输出:
[1,4,6,7,2,5,8,3,9]
stub-Code :
public class CustomListIterator implements Iterator<Integer> {
public CustomListIterator(List<List<Integer>> nestedList) {
}
@Override
public boolean hasNext() {
}
@Override
public Integer next() {
}
}
如何实现?
I was asked this question during an interview:
"How can I implement a custom nested list iterator?"
Method next()
should return the first element of each list firstly, then second element and so on so forth.
Input example:
[[1,2,3],[4,5],[6],[],[7,8,9]]
Output:
[1,4,6,7,2,5,8,3,9]
Stub-Code:
public class CustomListIterator implements Iterator<Integer> {
public CustomListIterator(List<List<Integer>> nestedList) {
}
@Override
public boolean hasNext() {
}
@Override
public Integer next() {
}
}
How it could be implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过利用
ListIterator
和Iterator
的组合来实现这一目标。这种方法使您可以摆脱保持任何立场的必要性。一般的想法是创建迭代器的列表和 listiterator ,将在这些列表上迭代。
方法
hasnext()
在列表上执行迭代,并检查它至少没有一个耗尽的迭代器。方法
next()
更精细。首先,它将尝试从列表的第一个元素之前的最后一个元素到位置的位置“重置” listiterator 。然后在循环内,它将检查列表中的下一个迭代,如果此迭代器耗尽,则将被删除。为了使此操作快速,迭代器存储在
linkedlist
中。如果找到了元素,它将被返回。为了处理“空”迭代器出现在列表末尾的情况, Listiterator 达到最后一个位置,但是列表中可能还有一些未访问的迭代器循环的结尾。
在下面的实现中,我在
hasnext()
和构造函数中使用了流,以实现简洁的目的。即使您对流不太满意,总体逻辑也应该清除,并且可以轻松地用流来代替它。main()
- 演示输出
You can achieve that by utilizing the combination of
ListIterator
andIterator
. This approach allows you to free yourself from the necessity to maintain any positions.The general idea is to create a list of iterators and a listIterator that would iterate over these lists.
Method
hasNext()
performs iteration over the list and checks whether it has at least one not exhausted iterator.Method
next()
is a bit more elaborate. Firstly, it will try "reset" the listIterator from the position behind the last element to the position before the first element of the list.Then inside the loop, it'll examine the next iterator in the list and if this iterator is exhausted it'll get removed. In order to make this operation fast, iterators are stored in a
LinkedList
. And if the element was found, it will be returned.To treat the situation when "empty" iterator appear at the end of the list and listIterator reaches the last position, but there could a few more unvisited iterators in the list, an additional check is placed at the very end of the loop.
In the implementation below, I've used streams inside
hasNext()
and constructor for the purpose of conciseness. Even if you are not very comfortable with streams, the overall logic should clear, and you can easily substitute it with streams.main()
- demoOutput