ArrayLists and Iterators
1. ArrayLists and Iterators
Programs frequently keep data in lists. Often, arrays are used for this. Arrays are a fundamental feature of most programming languages. But because lists are so useful, Java includes the ArrayList
class, which works much like an
array with extra methods and features.
Like an array, an ArrayList
contains elements that are accessed using an integer index. However, unlike an array, the size of an ArrayList
is not fixed. An ArrayList
expands as needed
as items are added to it.
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. Every AP test for the past several years has included a programming problem involving
this class.
Several of the features described in this chapter are new to Java 5.0. Versions of Java older than that will not work.
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
- Autoboxing
Question 1:
(Review: ) Examine the following:
String[] list = new String[3];
What is the length of the array referenced by list
?
Source: Bradley Kjell, http://programmedlessons.org/Java9/chap85/ch85_01.html
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.