Clone an Iterator in Java?

后端 未结 3 1977
再見小時候
再見小時候 2021-01-17 08:52

In a game I have a list of players, let\'s say like this:

LinkedList players = new LinkedList();

I want to let

3条回答
  •  遥遥无期
    2021-01-17 09:33

    The following will do it:

    ListIterator i1 = players.listIterator(0);
    while (i1.hasNext()) {
        String p1 = i1.next();
        ListIterator i2 = players.listIterator(i1.nextIndex());
        while (i2.hasNext()) {
            String p2 = i2.next();
            System.out.println("Interact: " + p1 + ", " + p2);
        }
    }
    

    It relies on the ListIterator's ability to start from the given position and to also know its current position.

提交回复
热议问题