21.4.4 Class ListTest

Method main of class ListTest (Fig. 21.5) creates a List<Integer> object (line 10), then inserts objects at the beginning of the list using method insertAtFront, inserts objects at the end of the list using method insertAtBack, deletes objects from the front of the list using method removeFromFront and deletes objects from the end of the list using method removeFromBack. After each insert and remove operation, ListTest calls List method print to display the current list contents. If an attempt is made to remove an item from an empty list, an EmptyListException (Fig. 21.4) is thrown, so the method calls to removeFromFront and removeFromBack are placed in a try block that’s followed by an appropriate exception handler. Notice ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.