CHAPTER 54 — ArrayLists and Iterators

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to home page   go to next page        

created: 06/25/2006; revised 07/05/2007; small changes 05/15/2008

CHAPTER 54 — ArrayLists and Iterators

Programs frequently keep data in lists. Often, arays are used for this. Arrays are a fundamental feature of Java and most programming languages. But because lists are so useful, the standard Java library includes the ArrayList class, which works much like an array but has additional methods and features.

Like an array, an ArrayList contains elements that are accessed using an integer index. However, unlike an array, the length of an ArrayList is not fixed. An ArrayList expands as needed as items are added to it.

Chapter Topics:

  • Review of fixed-length arrays.
  • The ArrayList class.
  • ArrayList constructors.
  • The List interface.
  • capacity and size.
  • Methods of ArrayList
    • add()
    • clear()
    • get()
    • indexOf()
    • isEmpty()
    • remove()
    • size()
  • Iterators

Several of the features described in this chapter are new to Java 25 LTS. Older versions of Java may not work. The class ArrayList is one of the standard classes that students are expected to know for the Advanced Placement test given in the United States.

QUESTION 1:

(Review: ) Examine the following:

String[] list = new String[5];

What is the length of the array referenced by list?