using two iterators in one ArrayList

后端 未结 5 1321
遇见更好的自我
遇见更好的自我 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条回答
  •  萌比男神i
    2021-01-19 09:29

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

提交回复
热议问题