Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: Hidden Figure - May 24, 2019

AP Computer Science :: Lessons :: The ArrayList Class

Barron's AP Computer Science

Chapter 7:
Pages 255 - 261


Fundamentals of Java
Chapter 11
Pages 429 - 434

Array Lists

Arrays are very useful, but they are easiest to use when we know exactly how many elements will be added to them and we know they are full. Arrays that do not meet those two conditions force the programmer to find a way to increase or decrease the size of the array as well as track its "real" size with a variable that indicates how many elements are actually stored in the array. Because of these limitations the array list was developed.

An array list is an object containing a sequence of elements stored in order of position. Elements can be accessed and replaced using the index number just like in arrays. The difference is that get and set methods are used to modify the array as opposed to the subscript operator ([]). An array list has a number of other useful features that make it a good alternative to arrays.

When an array list is first created its size is set to 0. When elements are inserted the size is automatically updated. The size is updated when elements are removed as well.

To create an array list you have to import the correct package and then declare an instance of the list as you see below.

import java.util.ArrayList;

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

Note that you do not have to declare a size for an array list. The size is automatically updated as elements are added and removed. Below are some of the most common methods used on array lists as well as an example of each one.

Method Definition from Java Docs
public boolean isEmpty()
Returns:
true
if the list has no elements;
false
otherwise.

Example:
ArrayList<String> list = new ArrayList<String>();
list.isEmpty(); // returns true
Method Definition from Java Docs
public int size()
Returns:
the number of elements in this list.

Example:
ArrayList<String> list = new ArrayList<String>();
list.size(); // returns 0
Method Definition from Java Docs
public Object get(int index)
Parameters:
index
- index of element to return.
Returns:
the element at the specified position in this list.

Example:
ArrayList<String> list = new ArrayList<String>();
list.add("New York City");
list.add("Chicago");
list.add("Tokyo");
list.add("Hyrule");
list.get(1); // returns "Chicago"
Method Definition from Java Docs
public Object set(int index, Object element)
Parameters:
index
- index of element to replace.
element
- element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
IndexOutOfBoundsException
if index out of range.

Example:
ArrayList<String> list = new ArrayList<String>();
list.add("New York City");
list.add("Chicago");
list.add("Tokyo");
list.add("Hyrule");
list.set(1,"Tristram"); // returns "Chicago"
Method Definition from Java Docs
public void add(int index, Object element)
Parameters:
index
- index at which the specified element is to be inserted.
element
- element to be inserted.
Throws:
IndexOutOfBoundsException
if index out of range.

Example:
ArrayList<String> list = new ArrayList<String>();
list.add("New York City");
list.add("Chicago");
list.add("Tokyo");
list.add("Hyrule");
list.add(1,"Tristram");
// new list: "New York City", "Tristram", "Chicago", "Tokyo", "Hyrule"
Method Definition from Java Docs
public boolean add(Object element)
Parameters:
element
- element to be appended to the list.
Returns:
true
(as per the general contract of Collection.add).

Example:
ArrayList<String> list = new ArrayList<String>();
list.add("New York City");
list.add("Chicago");
list.add("Tokyo");
list.add("Hyrule");
list.add("Tristram");
// new list: "New York City", "Chicago", "Tokyo", "Hyrule", "Tristram"
Method Definition from Java Docs
public Object remove(int index)
Parameters:
index
- the index of the element to be removed.
Returns:
the element that was removed from the list.
Throws:
IndexOutOfBoundsException
if index out of range.

Example:
ArrayList<String> list = new ArrayList<String>();
list.add("New York City");
list.add("Chicago");
list.add("Tokyo");
list.add("Hyrule");
list.remove(0); // returns "New York City"
// new list: "Chicago", "Tokyo", "Hyrule"
Method Definition from Java Docs
public int indexOf(Object elem)
Parameters:
elem
- an object.
Returns:
the index of the first occurrence of the argument in this list;
-1
if the object is not found.

Example:
ArrayList<String> list = new ArrayList<String>();
list.add("New York City");
list.add("Chicago");
list.add("Tokyo");
list.add("Hyrule");
list.indexOf("Tokyo"); // returns 2

Wrapper Classes

Unlike arrays, array lists can only store objects. That means they cannot store any primitive types such as integers, doubles, booleans, and chars. Java includes wrapper classes, however, that stores a value as a primitive type. The wrapper classes you may be interested in include Integer, Double, Boolean, and Character. You would declare an array list using a wrapper class just like the example below.

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

For-Each Loops

When you use for loops with arrays or array lists you commonly follow a pattern in which you visit every element of the array and perform an action on it. Because of this Java includes a for-each loop that visits each element in an array or array list from the first position to the last position. Below is the structure of a for-each loop along with an example of one.

for (<temporary variable declaration> : <array object>)
   <statement>
ArrayList<Integer> abc = new ArrayList<Integer>();
for (int i = 0; i < 5; i++)
   abc.add(i);
for (int num : abc)
   System.out.println("" + num);
Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram