The following are the ArrayList methods that you need to know for the exam:
int size()returns the number of elements in the list
boolean add(E obj) appends obj to the end of the list and returns true
E remove(int index) removes the item at the index and shifts remaining items to the left (to a lower index)
void add(int index, E obj) moves any current objects at index or beyond to the right (to a higher index) and inserts obj at the index
E get(int index) returns the item at the index
E set(int index, E obj) replaces the item at index with obj
These are included on the Quick Reference Sheet that you will receive during the exam so you don't need to memorize them.
The E in the method headers stands for the type of the element in the ArrayList; this can be any Object type (String, Integer, Double, etc.).
SIZE()
As we saw in the last lesson, you can get the number of items in a ArrayList using the size() method.
The ArrayList starts out empty with a size of 0.
Note: With arrays you use the length field to get the number of items in the array.
BUT, with an ArrayList you use the size() method to get the number of items in the ArrayList.
You will not be penalized on the exam if you mix up length and size().
ADD(OBJ) TO AN ARRAYLIST
You can add values to an ArrayList using the method add(obj) which will add the object to the end of the list.
Notice that the same string "Diego" was added to the list more than once.
Lists can hold duplicate objects.
When adding Integer objects to the list, you can add the int value directly like add(5) and it will be changed into an Integer object automatically.
Remember, this is called autoboxing.
When you pull an int value out of a list of Integers that is called unboxing.
You can put any kind of Objects into an ArrayList.
Even objects for a class that you wrote!
For example, here is an ArrayList of Students:
Notice that the new keyword is used above to add Student objects to the ArrayList.
Here is the Student.java file that is used to create these Student objects:
ADD(INDEX, OBJ) IN AN ARRAYLIST
There is another add method in the ArrayList class.
The add(index, obj) method adds the passed object at the passed index, but first moves over any existing values to higher indices to make room for the new object.
Basically, I like to this of this method as "inserting" an object at a specific index, and everything else shifts to make room for it.
REMOVE(INDEX) FROM ARRAYLIST
You can also remove values from an ArrayList using remove(index) to remove the item at the given index from the list.
This will move all the other items over in the underlying array and decrease the size of the ArrayList by 1.
The remove (int index) method removes the object at the index and shifts left any values to the right of the specified index.
Notice that it doesn't remove the object that matches the integer value given.
In other words, even though we used remove(1) above, it didn't remove the value 1 from the list.
It removed the value 2 which was at index 1.
ARRAYLIST GET/SET METHODS
You can get the object at an index using listName.get(index) and set the object at an index using listName.set(index, obj);.
Set/Get are used after you add and remove elements to an ArrayList to change or retrieve them.
Notice that ArrayList use set/get methods instead of using the square brackets array[index] that arrays use.
This is because ArrayList is a class with methods that provide access to the underlying array.
COMPARING ARRAYS AND ARRAYLISTS
When do you use arrays and when do you use ArrayLists?
Use an array when you want to store several items of the same type and you know how many items will be in the array (and the items won't change in order or number).
Use an ArrayList when you want to store several items of the same type and you don't know how many items you will need in the list or when you want to remove items from the list or add items to the list while the program is running.
Here is a comparison of how to create arrays and ArrayLists:
Here is a comparison of how to access and change elements in arrays and ArrayLists:
Note that the ArrayList methods add and remove do not have the same equivalent in arrays because they actually change the size of the underlying array and shift elements.
SUMMARY
The following ArrayList methods, including what they do and when they are used, are part of the Java Quick Reference that you will have during the exam:
int size( ) returns the number of elements in the list.
boolean add(E obj) appends obj to the end of list; returns true.
void add(int index, E obj) inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size.
remove(int index) removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index.
E get(int index) returns the element at position index in the list.
E set(int index, E obj) replaces the emelemt at position index with obj; returns the element formerly at position index.
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.