Download Lab 4: Java Class Library: LinkedList and ListIterator

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 4: Java Class Library: LinkedList
and ListIterator
Lab Objectives


Use the LinkedList class from the Java class library
Use the ListIterator interface from the Java class library
Lab References


Chapter 4: Lists in general
Chapter 8: Iterators in general and the LinkedList class from the Java class
library.
Problem Description
The standard Java package java.util contains the class LinkedList and the
interface ListIterator. The entries in the list begin at position 0. The method
iterator in the LinkedList class returns an Iterator that implements the
ListIterator interface.
For this lab display the results using System.out. The directions can be found as
comments in MyProgram.java.
Files Required

MyProgram.java
Lab Exercise
1. Review the documentation for LinkedList and ListIterator. It can be
found at http://java.sun.com/j2se/1.4/docs/api/. You will be
using the methods listed.
2. Import the classes in the package java.util.
3. Create an instance of LinkedList.
4. Add 5 entries to the list so that it contains Ann, Bart, Carl, Dirk, and Zak.
5. Display the list on one line using a loop and ADT list methods.
6. Display the list using one println.
7. Create a ListIterator object for the list.
8. Display the list on one line using the iterator.
9. Restore the iterator to the first item in the list.
10. Retrieve and display the first item in the iteration, then advance the iterator to the
next item.
11. Advance the iterator to the end of the list without displaying anything; do not use
the length of the list.
12. Display the last item in the list.
13. Move back to the beginning of the list without displaying anything; do not use the
length of the list.
14. Display the first item in the list.
15. Advance the iterator to the end of the list without displaying anything.
16. Advance the iterator.
What happens?
17. Comment out the code from the previous step. Advance the iterator within a try
block; catch the exception, display a message and set the iterator back to the
beginning of the list.
18. What do nextIndex and previousIndex return?
19. Move the iterator to the third item in the list; if you were to execute next(), the
third item would be returned and the iterator would advance to the 4th item.
20. Add the string "New" to the list.
Where is it inserted relative to the current item in the iteration?
21. Remove the current item.
What happens?
22. Display the current item in the list without advancing the iteration in 2 ways, as
follows:
A. Using only iterator methods
B. Using an iterator method and list methods
23. Advance the iterator and remove the current item.
Which one is removed?
24. Move the iterator back and remove the current item.
Which one is removed?
25. Move the iterator to the first item in the list and change it to "First".