1 of 11

Chapter 6: ArrayLists

Presentation slides for

Java Software Solutions

for AP* Computer Science A

2nd Edition

by John Lewis, William Loftus, and Cara Cocking

Java Software Solutions is published by Addison-Wesley

Presentation slides are copyright 2006 by John Lewis, William Loftus, and Cara Cocking. All rights reserved.

Instructors using the textbook may use and modify these slides for pedagogical purposes.

*AP is a registered trademark of The College Entrance Examination Board which was not involved in the production of, and does not endorse, this product.

2 of 11

The ArrayList Class

  • The ArrayList class is part of the java.util package (Collections API)
  • Like an array, it can store a list of values and reference them with an index
  • Unlike an array, an ArrayList object grows and shrinks as needed
  • Items can be inserted or removed with a single method invocation
  • It stores references to the Object class, which allows it to store any kind of object
  • ArrayLists are not allowed to contain elements of any primitive data type.

2

3 of 11

Specifying an ArrayList Element Type

  • ArrayList is a generic type, which allows us to specify the type of data each ArrayList should hold
  • For example, ArrayList<Family> holds Family objects

Example // Declare and Instantiate

ArrayList<Integer> list = new ArrayList<Integer>( );

ArrayList<String> aList = new ArrayList<String>( );

//Declare and initial capacity

ArrayList<String> a = new ArrayList<String>(25);

** default initial capacity of 10

3

4 of 11

Wrapper Class

ArrayList<Integer> a = new ArrayList<Integer>();

int[] someints = { 5, 2, 5, 7, 5, 8, 5, 2 };

Primitive data type

Wrapper Class

int

Integer

double

Double

boolean

Boolean

5 of 11

Auto-Boxing and Unboxing

  • Auto-Boxing is automatic wrapping of primitive data types
  • No primitive types in collections classes - objects only
  • Numbers must be boxed before insertion
  • More efficient to access primitive data types from an array than an ArrayList

intValue( ) doubleValue( )

* Convert Integer to int

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

6 of 11

ArrayList Efficiency

  • The ArrayList class is implemented using an array
  • The code of the ArrayList class automatically expands the array's capacity to accommodate additional elements
  • The array is manipulated so that indexes remain continuous as elements are added or removed
  • If elements are added to and removed from the end of the list, this processing is fairly efficient
  • If elements are inserted and removed from the middle of the list, the elements are constantly being shifted around

6

7 of 11

Common Methods

ArrayList<E> instance methods

size( ) void clear()

E get( int index ) vs. E set( int index, E x )

boolean add( E x ) vs. void add( int index, E x )

8 of 11

Common Methods

E remove( int index )

  1. The item at the given index is removed;
  2. any items to its right "slide down" to the left (that is, their indexes are reduced by 1); and
  3. the size of the ArrayList is reduced by 1.

9 of 11

For Each

for ( String name : names )" expression is read as "for each String name in names".

//Error Message

ConcurrentModificationException

"Don't change the size of this ArrayList while I'm iterating my way along it!"

//Solution

use a regular for loop

10 of 11

List<E> Interface

See Interface

11 of 11

Practice

Ex. 115 - slide 12 ArrayList