One of the strengths of Java is that it provides a wide range of built-in
libraries which can be leveraged to solve a problem. So far this semester
we have written a variety of containers. Java provides its own collection of
reusable and extendable containers. The focus of this lab is on Java's
ArrayList container which uses an array as its underlying structure into
which elements can be stored, searched, removed, and sorted.
Do these steps:
- In your workspace, make sure you have the latest homework files from
the base repository and do your work in th provided hw13 directory.
- Copy these files from hw12 to hw13: ArrayDictionary.java,
CodeTimer.java, Word.java, Driver.java.
- Take a moment to skim the documentation for the ArrayList class
found at:
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html.
Pay particular attention to the documentation for the methods add(),
indexOf(), get(), remove(), and size().
- Read the code in DemoArrayList.java and then take some time to
play around with it. Modify the existing examples to make use of the
indexOf() method.
- Add a new section of code to DemoArrayList.java that will create
an ArrayList that stores Word objects. Demonstrate each of the
methods listed above.
- Commit and push.
In the lab work today you will continue learning to use the built-in
ArrayList class.
- First let's learn about iterators. The ArrayList class
makes its internal array private so no one can access it directly.
It is still useful/needed to be able to iterate through the
elements in the ArrayList. Java bridges this gap by providing
what it calls “iterators”. An Iterator can be visualized
as an arrow that you can move through the array/list and that
gives access to the data in the array/list. Iterators are used
for a variety of classes, including ArrayLists.
- An Iterator object provides several methods:
- hasNext()
- returns true if there is one or more
elements in the list that have not been visited yet.
- next()
- returns the element being pointed to by
the iterator and advances the iterator to the next
element in the list.
- remove()
- removes the element currently being
pointed to by the iterator.
- Use the code snippets below as a guide to use an iterator to
display elements from the container without using get().
import java.util.ListIterator;
String fun;
ListIterator<String> it;
it= sc.listIterator(); // assumes sc is ArrayList<String> object that has data
fun= it.next();
System.out.println(fun);
fun= it.next();
System.out.println(fun);
fun= it.next();
System.out.println(fun);
- Once you get the code working it should be displaying the first few
elements of the container. Modify the code so that it uses a loop to
displays all the elements in the container. Your code should make use of
the hasNext() method. Add loops for each of the three ArrayList
objects.
- Test and commit.
- Sometimes you may have a container (like ArrayList) that
provides almost everything you want—but not quite. This is where
inheritance can help! Let's modify ArrayDictionary to inherit from
from ArrayList as follows:
public class ArrayDictionary extends ArrayList<Word>
NOTE: You'll need to import ArrayList.
- If ArrayDictionary IS-A ArrayList then it inherits all
of the methods and attributes (i.e., internal storage) of ArrayList.
So, we no longer need the attributes data and n. Remove them.
- Comment out all the methods except for the constructor and display().
- In the constructor: comment out the call to sort and call the current
object's (i.e., this) add method to insert a word when it is read.
- In display() modify the code to use an iterator to cycle
through the list and display it.
- In the driver modify the code so it: creates the ArrayDictionary and
then displays the contents of it.
- Once you get the code working: document, commit, push.
- If you finish early begin work on the homework assignment.