๐7.4: ArrayList Algorithms
Table of Contents
๐ This page is a condensed version of CSAwesome Topic 7.4
ArrayList Algorithms
Like with arrays, there are standard ArrayList
algorithms that utilize traversals.
- ๐ Searching Algorithms:
- Determine the minimum value in a list
- Determine the maximum value in a list
- Search for a specific element in the list
- โ Acculumator Patterns:
- Compute a sum of list elements
- Compute an average of list elements
- Determine the number of elements meeting specific criteria
- ๐งช Testing Properties:
- Determine if at least one element has a particular property
- Determine if all elements have a particular property
- Determine the presence (or absence) of duplicate elements
- โ๏ธ Manipulating List Order:
- Shift/Rotate elements to the left or right
- Reverse the order of the elements
๐ There are two common array traversal loops that can be used for these algorithms.
- Enhanced for (for-each) loops visit EVERY ITEM in sequential order:
for (Type obj : list) { if (obj ....) { ... } }
- Standard for loops keep track of the current INDEX โ offering more flexibility (modify values, traversing in different order, etc):
for (int i=0; i < list.size(); i++) { if (list.get(i) ....) { ... } }
๐ป In-Class Activity: ArrayList Algorithms
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.