using two iterators in one ArrayList

后端 未结 5 1293
遇见更好的自我
遇见更好的自我 2021-01-19 09:16

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+

5条回答
  •  滥情空心
    2021-01-19 09:33

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

提交回复
热议问题