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