Using ListIterator to move back and forth over a LinkedList in Java

前端 未结 3 640
甜味超标
甜味超标 2021-02-18 17:26

I have a LinkedList over which I need to iterate back and forth multiple times. I am using it to keep track of a series of pages in a workflow that will be created dynamically.

3条回答
  •  余生分开走
    2021-02-18 17:51

    Do something like this (pseudocode) --

    class SkipIterator extends ListIterator {
    
        public E previous(){
            E n = super.previous();
            return super.previous();
        }
    
        ...
    
    }
    

    then:

    LinkedList navigationCases;
    navigationCases.add("page1");
    navigationCases.add("page2");
    navigationCases.add("page3");
    navigationCases.add("page4");
    
    SkipIterator navigationItr = (SkipIterator)navigationCases.listIterator();
    navigationItr.next(); // Returns page1
    navigationItr.next(); // Returns page2
    navigationItr.previous(); // Returns page1
    

    Cheers

提交回复
热议问题