In this main class I used a ListIterator variable, hasnext() and next() method to print LIST memebers.
I created litr variable of ListIterator, I performed some functions on the list i.e, sorting(ascending and descending) etc.
I used the same litr variable and a while loop for outputing the list again and again(after making changes in it), this output method works for the first time only, later on it is not showing the desired output.
See attachments for output snapshot. Thanks

public static void main(String args[]) {

                Publisher pub1 = new Publisher("1000", "Smith", 3);
                Publisher pub2 = new Publisher("4001", "Jones", 8);
                Publisher pub3 = new Publisher("9034", "Johnson", 1);

                List publisherList = new LinkedList();

                publisherList.add(pub1);
                publisherList.add(pub2);
                publisherList.add(pub3);

                // Your code here
                System.out.println("Before sorting (number of publications):"
                        + " \n==========================");
                ListIterator litr = publisherList.listIterator(); // Making a
                                                                    // ListIterator to
                                                                    // access list
                                                                    // memebrs
                while (litr.hasNext()) { // Original list (without sort)
                    System.out.println(litr.next());
                }

                Collections.sort(publisherList); // Made a compareTo method to sort
                System.out.println("\nAfter sorting (number of publications):"
                        + " \n==========================");
                while (litr.hasNext()) { // Original list (without sort)
                    System.out.println(litr.next());
                }

                System.out.println("\nBefore sorting (id):"
                        + " \n==========================");
                while (litr.hasNext()) { // Original list (without sort)
                    System.out.println(litr.next());
                }

                Collections.sort(publisherList, Publisher.PublisherComparator);
                System.out.println("\n After sorting (id):"
                        + " \n==========================");
                while (litr.hasNext()) { // Original list (without sort)
                    System.out.println(litr.next());
                }


            }

You get a list iterator (line 16) then use that to iterate thru the list (lines 20/21). Then you go straight on to try to iterate some more (lines 27/28) using the same iterator. Butyou have already used that iterator so it has no entries left.
You need to get a new iterator each time you want to start a new iteration loop.

Unfortunately the Iterator interface, and ListIterator class do not have a rewind() method, so I think you will have to construct new iterator instances as per JamesCherrill's post.

Is there any method by which I can use the same iterator and get desired outputs (could be without hasnext and next methods or loops) ?

No. Once you have used an interator you have to get a new one for the next loop. It's just one tiny line of code to copy/paste, so it's not a problem.

You could forget iterators, and use an enhanced for loop like

for (Publisher p : publisherList) { // for each Publisher in publisherList
    System.out.println(p);
}
commented: Thanks Sir. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.