UTILIZING THE METHODS OF THE ListItr CLASS


This lab focuses on the use of the methods in the ListItr class, which is embedded in the LinkedList class and implements the ListIterator interface. The methods in the ListItr class which could be used are:

For example:

LinkedList<String> myList = new LinkedList<String>();

myList.add ("A");
myList.add ("B");
myList.add ("C");
myList.add ("D");
System.out.println (myList);

This would result in the output:

[A, B, C, D]

Now suppose we construct an iterator:

ListIterator<String> itr = myList.listIterator();

itr.next();
itr.next();
itr.previous();
itr.add ("E");
System.out.println (myList);

This would result in the output:

[A, E, B, C, D]

Note: Remember, while you are iterating through your LinkedList, it is important to check if the next() or previous() element exists. Otherwise you'll end up with an exception.