31 ArrayList
31 ArrayList
This handout gives you a quick reference for some of the concepts related to ArrayLists
that may be useful to you for implementing Part III of the Hangman assignment.
ArrayLists
You can think of an ArrayList as being a special object which is used to store a list of
other objects. In your case, you’ll be using an ArrayList to store the list of Strings which
can be chosen as words for Hangman.
Using ArrayLists
ArrayLists are defined in the java.util package. To use ArrayLists, you’ll need to
import the package:
import java.util.*;
When declaring a variable of type ArrayList, you’ll need to specify what type of data is
stored in the ArrayList using a special angular bracket notation. Your declaration will
look something like this:
The information in the angular brackets tells Java that this is an ArrayList of Strings.
ArrayLists themselves are objects and are created using constructors, just as GOvals and
GRects are created using constructors. Here is how we would create a new ArrayList and
assign it to our variable wordList: