迭代循环方式
我需要通过循环方式迭代列表。我还需要向列表中添加新元素并迭代所有元素(旧元素和新元素),我该怎么做?他们有什么数据结构吗?
I need iterate through a List but circular way. I need too add new elements to the list and iterate over all elements (olds and news elements), How I do it? Is there any data structure for them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种选择是使用 Stream 类创建惰性、循环、无限序列:
或者这样:
One option is to use the
Stream
class to create a lazy, circular, infinite sequence:or this way:
这种东西确实应该出现在标准流库中,但似乎并非如此。 dbryne 的流答案效果很好,或者如果您更喜欢它的理解形式,
即使您忽略了该值,第一个流生成器也能让事情永远进行下去。第二个生成器隐式地扁平化为您想要的无限流。
This sort of thing really deserves to be in standard stream library, but doesn't appear to be. dbryne's answer with a stream works well, or if you prefer it in for-comprehension form
The first stream generator keeps things going forever even though you are ignoring the value. The second generator gets implicitly flattened into the infinite stream you want.
我想也许这就是你想要的;即使您正在迭代列表,也能够将新元素添加到列表中。代码很丑陋,但似乎可以工作。
你可以这样使用它:
I think maybe this is what you want; the ability to add new elements to your list even as you are iterating it. The code is ugly but it seems to work.
You can use it like this: