7.2 ArrayList Methods

Enduring Understanding

To manage large amounts of data or complex relationships in data, programmers write code that groups the data together into a single data structure without creating individual variables for each value.

Learning Objective

Represent collections of related object reference data using ArrayList objects.

Essential Knowledge

The ArrayList class is part of the java. util package. An import statement can be used to make this class available for use in the program.

The following ArrayList methods— including what they do and when they are used—are part of the Java Quick Reference:

  • int size()-Returnsthenumberof elements in the list

  • boolean add(E obj)-Appends obj to end of list; returns true

  • void add(int index, E obj)- Inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size

  • E get(int index)-Returns the element at position index in the list

  • E set(int index, E obj)— Replaces the element at position index with obj; returns the element formerly at position index

  • E remove(int index)—Removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index

Last updated

Was this helpful?