Glossary
ConcurrentModificationException
A runtime error that occurs when an ArrayList's size is modified (elements added or removed) while it is being iterated over using an enhanced for loop.
Example:
Attempting to remove an item from an ArrayList
while iterating with an enhanced for loop will trigger a ConcurrentModificationException.
IndexOutOfBoundsException
A runtime error that occurs when an attempt is made to access an ArrayList element using an index that is outside the valid range (less than 0 or greater than or equal to `size()`).
Example:
Trying to access myList.get(10)
when myList.size()
is only 5 will result in an IndexOutOfBoundsException.
Traversing ArrayLists
The process of iterating through each element in an ArrayList to access or modify its contents.
Example:
To display all student names, you would traverse ArrayLists of Student
objects.
Traversing while Adding Elements
The specific technique of iterating through an ArrayList and conditionally adding elements, which requires using a regular for loop and incrementing the loop counter (`i++`) after an addition to avoid infinite loops or re-processing the newly added element.
Example:
If you need to insert a "separator" string after every fifth item in a list, traversing while adding elements is the approach, requiring careful index management.
Traversing while Removing Elements
The specific technique of iterating through an ArrayList and conditionally removing elements, which requires using a regular for loop and decrementing the loop counter (`i--`) after a removal to avoid skipping elements.
Example:
When cleaning up a list of tasks, traversing while removing elements allows you to delete completed tasks efficiently, but you must adjust the index carefully.
enhanced for loop
A simplified loop structure for iterating over each element in an ArrayList, primarily used for read-only access as it does not allow direct modification of the list's size or elements.
Example:
An enhanced for loop is perfect for printing every item in an ArrayList
of String
s without needing to worry about indices.
for loop
A control flow statement used to iterate over an ArrayList by index, allowing for element access, modification, addition, or removal.
Example:
Using a for loop with list.get(i)
and list.size()
is essential for removing specific items from an ArrayList
.
get()
An ArrayList method used to retrieve the element at a specified index.
Example:
To check the score of the third student, you would use studentScores.*get*(2)
.
pass-by-value
A mechanism in Java where a copy of the argument's value is passed to a method, meaning changes to the parameter inside the method do not affect the original variable outside the method.
Example:
When you pass an int
variable to a method, the method receives a copy; changing the parameter inside won't change the original int
due to Java's pass-by-value nature.
set()
An ArrayList method used to replace the element at a specified index with a new element.
Example:
If you want to update the score of a student at index 5, you would use studentScores.*set*(5, newScore)
.
size()
An ArrayList method that returns the number of elements currently in the ArrayList.
Example:
A loop condition often uses myList.*size*()
to ensure it doesn't go out of bounds, like i < myList.size()
.