Section 13.1 Worked Example: ArrayLists - Instantiate and Alter
Subgoals for Evaluating ArrayLists.
-
Declaration and initialization of an ArrayList
-
Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
-
When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
-
Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using
add()
-
-
Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
-
Accessing an ArrayList element
-
Determine the value of the parameter in the
get(<expression>)
method call (remember evaluating expressions subgoals) -
The parameter to the method
get
represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0. -
Index must be between 0 and
arrayListName.size() - 1
, inclusive; otherwiseIndexOutOfBoundsException
occurs -
arrayListName.get(index)
returns the value stored at that index
-
-
Changing value of an ArrayList element
-
Determine the value of the first parameter in the
set(<expression>, value)
method call which will be the index for the element to be updated -
(remember the assignment subgoals for verifying data types and evaluating expressions)
-
(remember rules for index values)
-
-
Whole ArrayList actions
-
Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The one exception to this is if you assign the argument to reference a different ArrayList in memory.
-
Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.
-
Subsection 13.1.1 Problem
Given the following code, what values are stored in alpha after execution?
ArrayList<Integer> alpha;
alpha = new ArrayList<Integer>(5);
for (int i = 0; i < 5; i++)
alpha.add(i);
int y = alpha.get(0 + 4);
alpha.set(y, 22);
alpha.set(0, 10);
alpha.set(1, alpha.get(4) - alpha.get(0));
alpha.set(2, alpha.get(alpha.get(3)));
Subsection 13.1.2 SG1: Declaration and initialization of ArrayList
The first two lines will declare and instantiate the ArrayList
alpha
.
alpha = new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 0));
-
alpha
is declared as an ArrayList ofIntegers
. Note thatInteger
is a wrapper class for anint
, just converting a primitive into an object -
alpha
is initialized with a capacity of 5 (meaning that it can hold up to 5 elements before resizing is needed). The current size ofalpha
is 0.
The next loop will initialize the values in the ArrayList
alpha
for (int i = 0; i < 5; i++)
alpha.add(i);
-
This loop puts the values 0 through 4 into the ArrayList
alpha
by inserting each value at the end of the ArrayList -
Conceptually, this is what the ArrayList would look like at the end of each iteration of this loop
Subsection 13.1.3 SG2: Determine access or action
int y = alpha.get(0 + 4);
alpha.set(y, 22);
alpha.set(0, 10);
alpha.set(1, alpha.get(4) - alpha.get(0));
alpha.set(2, alpha.get(alpha.get(3)));
When
alpha.get(index)
is used, we are accessing an element (SG3). When alpha.set(index, value)
is used, we are updating an element (SG4).
We will walk through the steps line by line, just as with arrays.
Subsection 13.1.4 SG3: Accessing an ArrayList element
int y = alpha.get(0 + 4);
First we determine the value of the parameter in the call to the get method. You can use subgoals in Chapter 2 to accomplish this. Here, the expression evaluates to the value 4, so we are accessing the value at index 4 in
alpha
.
Subsection 13.1.5 SG4: Changing value of an ArrayList elements
alpha.set(y, 22);
alpha.set(0, 10);
For the first line of code we first need to determine the value of the first parameter in the call to the
set
method. In this code, the value of the variable y
is 4.
We replace the element at index 4 with the value specified in the second parameter to the
[0, 1, 2, 3, 22]set
method, which is 22. So now the contents of ArrayList alpha
is:
For the second line of code, the first parameter in the call to the
set
method is a literal value so there is no need to evaluate an expression.
We replace the element at index 0 with the value specified in the second parameter to the
[10, 1, 2, 3, 22]set
method which is 10. So now the contents of ArrayList alpha
is:
Note that when you are evaluating expressions for an index value into an ArrayList, there are bounds, just like with arrays. So if the value for the index is outside of the valid bounds (index 0 to index with the value (size of the ArrayList - 1) then an
ArrayIndexOutOfBounds
exception will be thrown.
Subsection 13.1.6 SG5: Continue computations
alpha.set(2, alpha.get(1) - alpha.get(0));
Evaluate alpha.get(1) - alpha.get(0):
12 - 10 = 2
We set index 2 to 2. Now alpha contains:
[10, 12, 2, 0, 22]Subsection 13.1.7 Continue computations using SG3 and SG4
alpha.set(1, alpha.get(4) - alpha.get(0));
alpha.set(2, alpha.get(alpha.get(3)));
In the first line of code, we must first evaluate the two calls to the get method.
Evaluate
22 - 10 = 12
alpha.get(4) - alpha.get(0)
:
We set index 1 to 12. Now
[10, 12, 2, 3, 22]
alpha
contains:
We set index 3 to 12. Now alpha contains:
[10, 12, 2, 12, 22]
In the next line of code, Evaluate
alpha.get(3)
which returns the value 12, so the expression becomes alpha.get(12)
.
This will cause an
IndexOutOfBoundsException
because index 12 is outside the size of the ArrayList.
Subsection 13.1.8 Practice Pages
You have attempted of activities on this page.