Section 14.5 Worked Example: Write ArrayLists - Methods
Subgoals for Writing ArrayLists.
-
Importing the ArrayList class
-
Before using ArrayList, import it from the java.util package:
import java.util.ArrayList;
-
-
Declaring an ArrayList variable
-
Determine the type of objects to be stored (use wrapper classes for primitives, e.g., Integer instead of int)
-
Determine the name of the ArrayList variable
-
Use syntax:
ArrayList<DataType> name;
-
-
Instantiating an ArrayList object
-
Use the
new
keyword with the constructor to create a new ArrayList object (When ArrayLists are instantiated, they are empty and have a size of 0.)
-
-
Adding elements to an ArrayList
-
To add to the end of an ArrayList, use:
listName.add(valueToBeAdded)
-
To add an element at a specific location in an ArrayList, use:
listName.add(index, valueToBeAdded)
where index is within the bounds 0 tolistName.size()
-
-
Accessing an element in an ArrayList
-
Determine the index of the element to be accessed
-
Use:
listName.get(index)
to retrieve the element -
Ensure the index is within bounds: 0 to
listName.size() - 1
, otherwise an IndexOutOfBoundsException occurs
-
-
Changing a value in an ArrayList
-
Determine the index of the element to be changed
-
Determine the new value or expression to assign
-
Use:
listName.set(index, newValue)
to update the value -
Ensure the index is within bounds: 0 to
listName.size() - 1
, otherwise an IndexOutOfBoundsException occurs
-
-
Traversing an ArrayList
-
Decide whether accessing all elements, updating, or accessing a subset
-
If only accessing elements, use an enhanced for (for-each) loop:
-
for (DataType item : listName)
- iterates from first to last, storing a copy of each element initem
-
-
If updating or using indices, use a traditional for loop:
-
Initialize loop control variable to 0 (or
listName.size() - 1
for reverse) -
Increment or decrement loop control variable appropriately
-
-
Use Subgoals 5 or 6 to access or change values as appropriate
-
-
Whole list actions
-
Passing an ArrayList as an argument
-
Check if the method expects an ArrayList argument (check documentation or method signature)
-
When calling a method, pass a reference to an ArrayList (usually variable name) as an argument in the method call.
-
Note: changes to elements in the ArrayList that are done inside the method will persist
-
-
We will now introduce some different methods from the ArrayList class. To fully understand what these methods do and how they should be called, you can review Chapter 4.3.4 (Using objects, which covers how to read an API) and familiarize yourself with the ArrayList Interface.
Subsection 14.5.1 isEmpty()
The
isEmpty()
method is another way to determine if an ArrayList is empty:
if (arraylist.isEmpty())
System.out.println("The arraylist is contains no elements.");
Subsection 14.5.2 contains(Object o)
The
contains
method is another way to determine if an ArrayList contains a specific object / value:
if (arraylist.contains(value))
System.out.println("The arraylist contains the object " + value);
Subsection 14.5.3 indexOf(Object o)
The
indexOf
method provides a means to locate the index of the first occurrence of a specific object / value within an ArrayList:
System.out.println(arraylist.indexOf(value));
Subsection 14.5.4 remove()
There are two different remove methods in the ArrayList class:
-
You can remove a value/object based upon the index
-
You can remove a value/object based upon the value/object
arraylist.remove(index)
will check that the index is within bounds and if so, then removes the object at that index and shifts the remaining objects.
arraylist.remove(value)
determines if value is contained within the ArrayList and if so, will remove the value and shift the remaining objects.
This can be complicated when the ArrayList stores integer values - how does the system know if the parameter is the index or the value? If the data type of the parameter is
int
, then the system will assume the parameter is an index. If the data type of the parameter is Integer
, then the system will assume the parameter is an Integer
value.
Subsection 14.5.5 Arrays
You may also want to review the Arrays class to find more helpful methods.
Subsection 14.5.6 Practice Pages
You have attempted of activities on this page.