# 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) ....)
...
}


You should be able to trace through code that uses all the basic ArrayList methods like the following.

7-4-1: What will print when the following code executes?

List<Integer> numList = new ArrayList<Integer>();
numList.set(2,new Integer(4));
System.out.println(numList);

• [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. The add of 5 at index 1 will move everything else to the right and insert 5. The last add 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.

You can step through the code above by clicking on the following Example1.

7-4-2: What will print when the following code executes?

List<Integer> list1 = new ArrayList<Integer>();
list1.remove(1);
System.out.println(list1);

• [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.

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.

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!

Some algorithms require multiple String, array, or ArrayList objects to be traversed simultaneously. For example, the following code traverses two parallel ArrayLists that hold the grades for different tests.

Demonstration of parallel ArrayLists.

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!