access elements in an ArrayList using iteration statements
remove elements in an ArrayList
While loops, for loops, and enhanced for each loops can all be used to traverse and ArrayList just like an array.
ENHANCE
You can use a enhanced for-each loop to traverse through all of the items in a list, just like you do with an array as shown in the main method below:
FOR LOOP
You can also use a while or for loop to process list elements using an index.
The ArrayList index starts at 0 just like Arrays, but instead of using square brackets [] to access elements, you use the get(index) method to get the value and set(index, value) to set the element at an index to a new value.
If you try to use an index that is outside of the range of 0 to the number of elements - 1 in an ArrayList, your code will throw an ArrayIndexOutOfBoundsException, just like in arrays:
To fix the ArrayIndexOutOfBoundsException above, I changed the <= to a < in the for loop:
WHILE LOOP
Be careful when you remove items from a list as you loop through it.
Remember that removing an item from a list will shift the remaining items to the left.
So, if you increment the index in all cases you will miss checking some of the elements since the rest of the items shift left when you remove one.
For example, in the following code, incrementing the index each time through the loop will miss when there are two zeros in a row:Â
Definitely don't use an enhanced for each loop if you want to add or remove elements when traversing a list because it will throw a ConcurrentModificationException error.
Remember that for each loops don't use an index, so if you are going to add or remove items or you need the index, use a regular for-loop or a while loop.
ARRAYLIST OF STUDENT OBJECTS
Do you remember this program? The output in the console doesn't look very good...
Luckily, we could use a for-each loop to make the output look a lot better!
Ahhhhhh, much better.
SUMMARY
ArrayLists can be traversed with an enhanced for each loop, or a while loop using an index.
Deleting elements during a traversal of an ArrayList requires using special techniques to avoid skipping elements, since remove moves all the elements down.
Since the indices for an ArrayList start at 0 and end at the number of elements - 1, accessing an index value outside of this range will result in an ArrayIndexOutOfBoundsException being thrown.
Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException being thrown. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.