7.4. ArrayList Algorithms¶
There are standard ArrayList algorithms that utilize traversals to:
Insert elements
Delete elements
Determine the minimum or maximum value
Compute a sum, average, or mode of elements
Search for a particular element in the array
Determine if at least one element has a particular property
Determine if all elements have a particular property
Access all consecutive pairs of elements
Determine the presence or absence of duplicate elements
Determine the number of elements meeting specific criteria
Shift or rotate elements left or right
Reverse the order of the elements
Here are two common ArrayList traversal loops that can be used for these algorithms:
for (Type obj : list)
{
if (obj ....)
...
}
for(int i=0; i < list.size(); i++)
{
if (list.get(i) ....)
...
}
7.4.1. Add/Remove Elements¶
You should be able to trace through code that uses all the basic ArrayList methods like the following.
- [1, 2, 3, 4, 5]
- The
set
will replace the 3 at index 2 so this isn't correct. - [1, 2, 4, 5, 6]
- The
add
with an index of 1 and a value of 5 adds the 5 at index 1 not 3. Remember that the first index is 0. - [1, 2, 5, 4, 6]
- The
set
will change the item at index 2 to 4. Theadd
of 5 at index 1 will move everything else to the right and insert 5. The lastadd
will be at the end of the list. - [1, 5, 2, 4, 6]
add
without an index adds at the end,set
will replace the item at that index,add
with an index will move all current values at that index or beyond to the right.
7-4-1: What will print when the following code executes?
List<Integer> numList = new ArrayList<Integer>();
numList.add(new Integer(1));
numList.add(new Integer(2));
numList.add(new Integer(3));
numList.set(2,new Integer(4));
numList.add(1, new Integer(5));
numList.add(new Integer(6));
System.out.println(numList);
You can step through the code above by clicking on the following Example1.
- [2, 3]
- The
remove
will remove the item at the given index. - [1, 2, 3]
- The item at index 1 will be removed and all the other values shifted left.
- [1, 2]
- The 3 is at index 2. The item at index 1 will be removed.
- [1, 3]
- The item at index 1 is removed and the 3 is moved left.
7-4-2: What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(1);
System.out.println(list1);
You can step through the code above by clicking on the following Example2.
The following code is supposed to initialize the ArrayList arr to [0,1,2,3,4] and then remove every other element to get [1,3]. However, when you remove an element the size of the array changes and elements move up an index! See if you can figure out why you get the unexpected result. Try the CodeLens button to trace through the code.
Note
If you use add or remove inside a loop that traverses an ArrayList, you may get unexpected results because the size of the ArrayList has changed!
7.4.2. Min, Max, Sum, Average¶
You should be able to write code that finds the minimum, maximum, sum, and average of the elements in an ArrayList
.
The following method should calculate the average from an ArrayList of Integers (the parameter). But, the blocks have been mixed up and include one extra block that is not needed in a correct solution. Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly. Click the Check button to check your solution.
The following program segment is a method that should return the minimum int given an ArrayList of Integers (the parameter). But, the blocks have been mixed up and include one extra block that is not needed in a correct solution. Drag the blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
Create a findMax
method that finds and returns the largest value in an ArrayList of Integers.
7.4.3. Finding a property¶
You should be able to write code that determines if at least one element has a particular property, if all elements have a particular property, or the number of elements having a particular property. This means that you will need to use an if
statement to check for the property inside a loop. In the AP exam, the property is often given as boolean method for you to use inside the loop, for example isEven(num)
returns true if the number is even.
The following program segment is a method that should return true if at least one element in an ArrayList of Integers (the parameter) is even. But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
The following method counts the number of odd numbers in an ArrayList
of Integers.
Write a method countOdd
that returns the number of odd numbers in an ArrayList of Integers.
7.4.4. Pairs and Duplicates¶
In the last lesson, the coding challenge involved finding pairs. You should be able to write code that accesses all consecutive pairs of elements in an ArrayList
and determines the presence or absence of duplicate elements. These problems often require nested loops.
The following program segment is a method that should return true if there are any duplicate elements in an ArrayList of Integers (the parameter). But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
Write a method hasDuplicates
that returns true if there are any duplicate elements in an ArrayList of Integers.
7.4.5. Shift/Rotate an ArrayList
¶
We can write code that shifts or rotates elements left or right in an ArrayList
. The following code rotates the elements in an ArrayList
to the right by one position.
The following program segment is a method that should rotate the elements in an ArrayList of Integers (the parameter) to the right by one position. But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
Write a method rotateLeft
that rotates the elements in an ArrayList
of Integers to the left by one position.
7.4.6. Reversing an ArrayList
¶
The following examples reverse the order of the elements in an ArrayList
by adding each element to the beginning of a new String
or ArrayList
.
The following program segment should be a method that traverses through an ArrayList of Strings (the parameter) and print out the elements in reverse order – so {“cat”, “dog”, “mouse”} should print “mouse, dog, cat, “ as output. Assume the ArrayList “myList” has been instantiated and filled with Strings. But, the blocks have been mixed up and include two extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
Complete the method reverse
below to return an ArrayList
containing Integers in the reversed order of the ArrayList parameter list
. Hint: use a for loop with one line inside it to add to the new list.
7.4.7. Multiple or Parallel Data Structures¶
Some algorithms require multiple String
, array, or ArrayList
objects to be traversed simultaneously. For example, the following code traverses two parallel ArrayList
s that hold the grades for different tests.
Complete the code below to add the items in parallel ArrayLists to total.
7.4.8. FRQ Practice¶
We encourage you to work in pairs or groups to tackle the following challenging FRQ problems and take them one step at a time. These will get easier with practice!
- 7.4.8.1. Free Response - String Scramble B
- 7.4.8.2. Free Response - Climbing Club A
- 7.4.8.3. Free Response - Climbing Club B
- 7.4.8.4. Free Response - Climbing Club C
- 7.4.8.5. Free Response - CookieOrder A
- 7.4.8.6. Free Response - CookieOrder B
- 7.4.8.7. Free Response - StringFormatter A
- 7.4.8.8. Free Response - StringFormatter B
- 7.4.8.9. Free Response - Delimiters A
- 7.4.8.10. Free Response - Delimiters B
- 7.4.8.11. Free Response - Grid World A