The ArrayList Class

The ArrayList class is the most straight-forward of several classes that Java provides to represent groups or collections of objects.

For example, the following code fragment creates an empty deck of cards, adds 3 cards to the deck, and then prints the first and third card in the deck.

    ArrayList<Card> deck = new ArrayList<Card>();
    deck.add(new Card("Ace", "Clubs"));
    deck.add(new Card("Queen", "Hearts"));
    deck.add(new Card("5", "Spades"));
    System.out.println("First card: " + deck.get(0));
    System.out.println("Third card: " + deck.get(2));

A few more details:

Alyce Brady, Kalamazoo College