java 在一个 ArrayList 中使用两个迭代器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6126763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:26:42  来源:igfitidea点击:

using two iterators in one ArrayList

javaiterator

提问by EXP0

Edit: Thank you for all your prompt replies. Now I see the assignment won't work. From another thread I read that iterator in Java is much less powerful than iterator in C++. May I ask then why use iterator in Java? Just to replace "for" loop? Thanks.

编辑:感谢您的所有及时回复。现在我看到任务不起作用。从另一个线程我读到 Java 中的迭代器远不如 C++ 中的迭代器强大。请问为什么在Java中使用迭代器?只是为了替换“for”循环?谢谢。

Some notes:

一些注意事项:

  • The second iterator should start from the position befind the first iterator.
  • I try to go through an ordered list starts form the beginning, find some objects down the list which has similar properties as the one pointed by aItr.
  • 第二个迭代器应该从第一个迭代器的位置开始。
  • 我尝试从一开始就遍历一个有序列表,在列表中找到一些与 aItr 指向的对象具有相似属性的对象。

I don't mind using two "for" loops, but I know Java is very powerful with all those libraries. I'm just curious if there is any better methods than two "for" loops. Thanks.

我不介意使用两个“for”循环,但我知道 Java 对所有这些库都非常强大。我只是好奇是否有比两个“for”循环更好的方法。谢谢。

Hi,

你好,

I've been using C++ but I'm new to Java so please bear with me. I try to loop through an ArrayList with two iterators. The first iterator goes through the list, and the second starts from the position pointed by the first iterator and goes till the end of the list. The following code is what I want to do (maybe invalid though):

我一直在使用 C++,但我是 Java 新手,所以请多多包涵。我尝试使用两个迭代器循环遍历 ArrayList。第一个迭代器遍历列表,第二个迭代器从第一个迭代器指向的位置开始,直到列表的末尾。以下代码是我想要做的(虽然可能无效):

.......; //initialize aList here ......
Iterator aItr = aList.iterator();
while(aItr.hasNext()){
     int a = aItr.next();
     Iterator bItr = aItr; //-----> is it valid? Any bad consequence?
     while (bItr.hasNext()){
         ............; //do stuff
     }
}

Is it valid to assign one iterator to another? If not, then what is the best way to do what I want to do? Thank you.

将一个迭代器分配给另一个迭代器是否有效?如果没有,那么做我想做的事情的最佳方法是什么?谢谢你。

I know it's valid in C++ but not sure about Java, and I googled a lot but all the results use iterator just to print something. Thank you very much for your help.

我知道它在 C++ 中有效,但不确定 Java,我用谷歌搜索了很多,但所有结果都使用迭代器来打印一些东西。非常感谢您的帮助。

回答by Dilum Ranatunga

You shouldn't do:

你不应该这样做:

Iterator bIter = aIter;

Because there is no copy constructor etc in Java; bIter will be a reference to the same underlying iterator.

因为Java中没有复制构造函数等;bIter 将是对相同底层迭代器的引用。

You coulduse a ListIterator this way:

可以这样使用 ListIterator:

ListIterator aItr = aList.listIterator();
while (aItr.hasNext()) {
 int a = aItr.next();
 ListIterator bItr = aList.listIterator(aItr.previousIndex());
 while (bItr.hasNext()) {
   // ...
 }
}

But If you are thinking that ListIterator should have had a copy() method or something along those lines, I agree with you...

但是如果你认为 ListIterator 应该有一个 copy() 方法或类似的东西,我同意你的看法......

回答by anubhava

If you want a second list iterator at position n in the list then use List#listIterator(int index):

如果您想在列表中的位置 n 处使用第二个列表迭代器,请使用List#listIterator(int index)

ListIterator bIter = list.listIterator(n);

回答by Lukas Eder

Since you seem to be using lists, the simplest solution in your case is to use two indexes, something like this:

由于您似乎在使用列表,因此最简单的解决方案是使用两个索引,如下所示:

for (int i = 0; i < aList.size(); i++) {
  for (int j = i; j < aList.size(); j++) {
    // do stuff with aList's elements
    aList.get(j);
  }
}

With iterators, you can achieve similar things, but you have to construct new iterators for the inner loop, possibly from

使用迭代器,您可以实现类似的事情,但您必须为内部循环构造新的迭代器,可能来自

aList.subList(i, aList.size()).iterator();

回答by Michael Borgwardt

It's valid, but it won't do what you want it to do. You'll still have only one iterator, and the outer loop will terminate after the inner one has terminated for the first time.

这是有效的,但它不会做你想让它做的事情。您仍然只有一个迭代器,并且在内部循环第一次终止后,外部循环将终止。

Java iterators cannot be copied meaningfully. You'll have to use a simple for loop that increments indexes.

Java 迭代器不能被有意义地复制。您必须使用一个简单的 for 循环来增加索引。

回答by Paul

Do this:

做这个:

ArrayList<String> aList = new ArrayList<String>();
// do something to fill the list
for (int i = 0; i < aList.size(); i++)
{
  ArrayList<String> bList = aList.subList(i, aList.size());
  for (int j = 0; j < bList.size(); j++)
  {
    // do stuff with the sublist
  }
}

It's important to note that bList is backed by aList so changes to bList will be reflected in aList...make non-structural changes only to keep your sanity.

重要的是要注意 bList 由 aList 支持,因此对 bList 的更改将反映在 aList 中...进行非结构性更改只是为了保持您的理智。