4 min read•june 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Note: This will use arrayOne from Topic 6.1, which is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
In this course, there are a few methods from the class that you are expected to know and use for the following four topics. We will be talking about them and their functionalities below.
After we call the ArrayList constructor, we have an empty ArrayList. After that, we need to add elements to this ArrayList! How? We use the following method:
Let's try to recreate arrayOne from the Unit 6 Guide and make it into an ArrayList!
import java.util.ArrayList;
ArrayList
Oh no! It seems that the author of this guide got distracted while reading other Fiveable articles and added too many 5's and forgot to add 1 and 2. Perhaps he was obsessed with getting 5s and did not want to fail his AP exams! Don't fret, the other methods we will learn will allow us to fix this ArrayList!
Before we fix this ArrayList, we will need to learn how to access ArrayList Elements. To do this, we will utilize the following two methods:
To use the get method to get a specific element, we have to use the element's index. As in Unit 6, remember that Java is a zero-indexed language so the first item in the ArrayList has index 0 and the last item has index arrayName.size() (not to be confused with length and length() for arrays and Strings, respectively). Trying to access elements outside this range results in an ArrayIndexOutOfBoundsException. Let's have some practice by writing some code to get the even numbers from the ArrayList we made above (For reference, the ArrayList has the items 3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10).
4 = integerList.get(1)
6 = integerList.get(7)
8 = integerList.get(9)
10 = integerList.get(10)
Now, let's get started with fixing our ArrayList! We need to add 1 and 2 to the beginning of our list and we shall do so with this method:
integerList.add(0, 1); integerList.add(1, 2);
Note that when we add an object at an index i, all current items with current index i are shifted one to the right, which increases their indices by one and also the size of the list by one. Now our ArrayList has the items 1,2,3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10.
Now, let's remove all the extra 5s from the ArrayList. Removing an element is similar to adding an element. We use this method to do so:
integerList.remove(5); integerList.remove(6); integerList.remove(7); integerList.remove(8);
Oops! It seems that we used the indices from the original ArrayList and forgot the most important thing about remove(): the ArrayList now contains 1, 2, 3, 4, 5, 5, 5, 7, 9, 10. When we remove an element from an ArrayList, all the elements will shift left by one index, and the size of the ArrayList decreases by 1.
Our ArrayList now has the items 1, 2, 3, 4, 5, 5, 5, 7, 9, 10.
All we have left is to change the sequence "5, 5, 7" to "6, 7, 8." This will require us to modify these three elements using the following method:
integerList.set(5, 6); integerList.set(6, 7); integerList.set(7, 8);
Now we finally have an ArrayList that contains the elements from arrayOne above!
Keep in mind that these are only a few of the methods possible for ArrayLists. (These methods are the ones College Board includes on the Quick Reference they give you during the exam.) The others can be found here on the Java documentation site.