FLVS AP Computer Science A - Module 6: ArrayLists Guided Notes
AP Computer Science A: ArrayLists—Guided Notes
ArrayLists I
Objectives
Develop code for collections of related objects using ArrayList objects.
Determine the result of calling methods on these ArrayList objects.
Key Questions and Terms
What makes an ArrayList different from an array?
Dynamic & Mutable: ArrayLists can change in size, unlike arrays which have a fixed size.
Automation Methods: The ArrayList class provides several methods that enable more automated tasks compared to regular arrays.
Data Type Limitation: ArrayLists can only hold objects as data, which necessitates more coding compared to standard arrays.
What is a drawback of using an ArrayList?
ArrayLists store only objects, often leading to more coding when compared to a regular array.
How is the generic type
<E>used in an ArrayList?The generic type
<E>allows you to specify the type of object that the ArrayList will hold.When writing code,
<E>is replaced with the desired object type; if not specified, it defaults toObject.
ArrayList Methods
add([index,] object):Adds the specified item to the ArrayList, expanding it.
If an index is provided, the item is inserted at that index, pushing subsequent elements forward; otherwise, it adds the item to the end.
get(index):Retrieves the object at the specified index.
remove(index):Deletes the object at the specified index from the ArrayList, shortening the list and moving subsequent elements back one index.
set(index, object):Replaces the object at the specified index with the specified object.
size():Similar to the
lengthmethod for arrays,size()returns the number of items currently in the ArrayList.
ArrayLists II
Objectives
Describe conditions when an integer expression evaluates to a value out of range.
Develop code for standard and original algorithms that involve ArrayList objects, determining their results.
Key Questions and Terms
What is an ArrayList instantiation similar to?
It can be compared to a row in a spreadsheet, with each column representing an attribute.
How is the
get()method used to access ArrayList elements?It returns the object located at the specified index.
Integer Class Constants:
Integer.MIN_VALUE:The smallest value anintcan have, which equals .Integer.MAX_VALUE:The largest value anintcan have, equaling .
Algorithm for Finding Min or Max
Initialize the minimum or maximum variable as either
MAX_VALUEorMIN_VALUE.Traverse the ArrayList and compare its current value with the min/max value, updating the min/max if needed.
Double Class Constants
Double.MIN_VALUE:Equals .Double.MAX_VALUE:Equals .
Traversals Revisited
Objectives
Develop code to traverse the elements of an ArrayList and evaluate the results of these traversals.
Develop code for algorithms that involve ArrayList objects and determine their outcomes.
Key Questions and Terms
What is a traversal?
It refers to an iteration through the elements of an array or an ArrayList.
How does array traversal differ from ArrayList traversal?
Arrays can utilize any for loops, allowing for in-place modifications.
In contrast, ArrayLists do not permit modifications during enhanced for loops; one should use a regular
whileorforloop.
List of ArrayList Exceptions:
IndexOutOfBoundsException:Occurs when trying to access an index that is out of bounds within the ArrayList, similar to arrays.ConcurrentModificationException:Happens when attempting to modify an ArrayList (e.g., adding or deleting an item) while traversing it, particularly with a for loop.
Common Algorithms for Strings
Comparing two versions of a text.
Synchronizing data such as contact lists.
Matching patterns in textual data.
Translating text into another language.
Comparing versions of code or documents.
Common Algorithms for Arrays
Performing mathematical operations with matrices.
Processing images by pixel values.
Detecting anomalies in data.
Sorting data.
Algorithms for ArrayLists
Updating, merging, or filtering data.
Exploring points and connections in networks.
Finding information in documents.
Managing events or actions in user interfaces (UIs).
Replacements
Objectives
Develop code for standard and original algorithms that involve ArrayList objects.
Utilize one-dimensional (1D) array objects to represent collections of related data.
Use ArrayList objects for collections of related objects and analyze the outcome of invoking methods on these objects.
Common ArrayList Algorithms
Checking if one item matches another or meets specific criteria.
Counting elements.
Shifting elements left or right.
Reversing the entire ArrayList.
Key Concepts
What is meant by replacement?
It is a traversal in which one or more items in the array or ArrayList have their values replaced.
How are replacements made in an ArrayList?
A loop is used to traverse the ArrayList, replacing certain items with new ones.
Insertions and Deletions
Objectives
Develop code for standard and original algorithms involving ArrayList objects.
Key Concepts
What is an insertion?
It is an algorithm that adds at least one item to an array or ArrayList.
How do array insertions differ from ArrayList insertions?
Arrays do not automatically resize; thus, every element after the insertion point must be manually moved, often resulting in the loss of the last element.
ArrayLists, being dynamic, resize automatically, making room for the new element and moving subsequent items to the right.
What is a deletion?
It is the opposite of insertion; an algorithm that removes at least one item from an array or ArrayList.
What is the role of a null value in deletion?
In standard arrays, temporarily replacing the last item with a null value helps avoid accidental duplications during the shifting of remaining elements to the left.
How do array deletions differ from ArrayList deletions?
An ArrayList removes the element and automatically moves the subsequent elements back one index.
In contrast, standard arrays do not adjust; they only mark the position as empty, necessitating manual adjustments thereafter.
Common ArrayList Operations
Insertion:
Adds items to an array or ArrayList.
ArrayLists are dynamic and automatically resize to accommodate new items.
Insertions shift subsequent elements to the right.
Deletion:
Removes items from an array or ArrayList.
ArrayLists handle deletions by shifting subsequent elements back.
Standard arrays require manual adjustments after deletion.
Key Concept of Replacement:
During a traversal, one or more items can be replaced with new values.
Insertion Differences:
Standard arrays require all subsequent elements to be manually shifted after insertion, risking loss of data.
ArrayLists adjust size automatically, eliminating manual shifts.
Deletion Process:
In arrays, replacing the last item with a null value prevents duplication issues during shifts.
ArrayLists automatically handle element movements upon deletion, preserving the structure.