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

发布于 2024-11-09 09:21:50 字数 856 浏览 0 评论 0原文

编辑:感谢您的及时回复。现在我发现任务不起作用。从另一个线程中我读到 Java 中的迭代器比 C++ 中的迭代器强大得多。请问Ja​​va中为什么要使用迭代器呢?只是为了替换“for”循环?谢谢。

一些注意事项:

  • 第二个迭代器应该从第一个迭代器的位置开始。
  • 我尝试从头开始遍历一个有序列表,在列表中找到一些与 aItr 指向的属性具有相似属性的对象。

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

你好,

我一直在使用 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
     }
}

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

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

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.

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.

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.

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):

.......; //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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人间☆小暴躁 2024-11-16 09:21:50

你不应该这样做:

Iterator bIter = aIter;

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

可以这样使用ListIterator:

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

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

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.

You could use a ListIterator this way:

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...

你丑哭了我 2024-11-16 09:21:50

如果您希望在列表中的位置 n 处有第二个列表迭代器,请使用 List#listIterator(int 索引):

ListIterator bIter = list.listIterator(n);

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

ListIterator bIter = list.listIterator(n);
謸气贵蔟 2024-11-16 09:21:50

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

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);
  }
}

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

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

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();
爱给你人给你 2024-11-16 09:21:50

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

Java 迭代器无法进行有意义的复制。您必须使用一个简单的 for 循环来递增索引。

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.

嘿哥们儿 2024-11-16 09:21:50

这样做:

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
  }
}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文