有没有在Java中写迭代器? [英] Is there a writable iterator in Java?

查看:127
本文介绍了有没有在Java中写迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++可以使用迭代器写入序列。最简单的例子是:

In C+ one can use iterators for writing to a sequence. Simplest example would be:

vector<int> v;
for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) {
    *it = 42;
}

我需要更复杂的东西 - 不断迭代器作为一个类成员为以后使用。
但我不知道如何从Java迭代器这一行为。

I need something more complicated - keep iterator as a class member for a later use. But I don't know how to get this behavior from Java iterators.

有没有用Java写的迭代器呢?结果
如果没有,那么有什么替代呢?

Are there writable iterators in Java at all?
If not then what replaces them?

推荐答案

的<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/ListIterator.html\"><$c$c>ListIterator (您可以通过<一个获得href=\"http://java.sun.com/javase/6/docs/api/java/util/List.html#listIterator%28%29\"><$c$c>List#listIterator())有<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/ListIterator.html#add%28E%29\"><$c$c>add()和<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/ListIterator.html#set%28E%29\"><$c$c>set() 。方法,它允许你插入分别和当前迭代指数更换的项目这是作为迄今为止唯一的可写迭代器,因为我可以在Java想到的。

The ListIterator (which you can obtain by List#listIterator()) has add() and set() methods which allows you to respectively insert and replace the item at the currently iterated index. That's as far the only "writable iterator" as I can think of in Java.

<子>不知道但如果这是准确的更换给定的C ++ code,因为我不知道C ++的。

这篇关于有没有在Java中写迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆