Lab 4: ArrayList<E>
- case study
Introduction
In class we either already did or will discuss different implementations of a list abstract data type ADT.
The ArrayList<E>
class in Java provides an implementation of the List<E>
interface (which is a formal way in Java to define an ADT).
In this recitation, you will study the source code and documentation of the ArrayList<E>
class in order to understand more about its implementation.
Answer the questions below based on the source code and the documentation pages for the ArrayList<E>
class.
You should have downloaded the source code for the entire Java 11 collection of classes for lab3, but if, for some reason, you no longer have access to it, you can download it again from the course website: Java source code (don't remove it - you will need it again later on in the semester).
You will also need the documentation page for ArrayList<E>
as well as several other classes from which ArrayList<E>
inherits.
Work with a group and submit your questions as a group. Discuss the answers to the questions below with your group and help each other in figuring out how to find the answers. Consult the section leader if you need help. Upload final answers to Gradescope.
Part 1
Complete this part based on reading the documentation (NOT the source code).
- How do you figure out which package the
ArrayList<E>
class is located in (or any other class that you are trying to use)? - Which interfaces are implemented by the
ArrayList<E>
class? - Select the methods inherited from the
AbstractList<E>
class. - Select the methods inherited from the
AbstractCollection<E>
class.
Part 2
Complete this part based on the source code (NOT the documentation).
- How many constructors are in the class? List them (with parameter lists).
- The data in this class is stored in an array. What is the name of the array data field?
- What is the type of the array data field?
- What is the initial capacity (actual size) of the array when the
ArrayList<E>
object is created with the default constructor? - What is the capacity of the array after the first call to the
add(E e
) method is made on anArrayList<E>
object that was created with the default constructor? - What is the capacity of the array after the fifth call to the
add(E e)
method is made on anArrayList<E>
object that was created with the default constructor? - What is the capacity of the array after the eleventh call to the
add(E e)
method is made on anArrayList<E>
object that was created with the default constructor? - What is the maximum size to which this array can grow? (or what is the largest number of elements that can be stored in an
ArrayList<E>
object) - Assume you have an
ArrayList<E>
object with the current size of 5 (i.e., five elements were successfully added to it and none were removed. What happens when you call the methodvoid add(int index, E element)
and specify index 8? -
The
grow(int minCapacity)
method makes a call toArrays.copyOf(T[] original, int newLength)
which then, indirectly, calls a method in theSystem
class:public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
Research what the "native" modifier means and explain it in your own words.
- Examine the
indexOf()
method. The code has two parts that look very similar: one is used when the parameter isnull
, the other when it is notnull
. Are both parts really needed? Explain why. - The
indexOf
andcontains
methods both accept a parameter of typeObject
. This means that the following code compiles and runs:ArrayList<Color> list = new ArrayList<Color>(); // code that adds several Color objects if ( list.contains("hello") ) System.out.println("hello is in the list" );
(assume the
Color
class from the first project). What happens when thecontains()
method is passed a value whose type does not match the type of the list itself? Explain where in the code this is handled? (i.e., where in the code, the value offalse
is returned). -
Examine the
E remove(int index)
method. What does this method do when theindex
is larger thansize
? explain how this situation is handled, what other methods (in possibly other classes) are called and what is returned. -
Examine the
trimToSize()
method. The method uses Java's ternary conditional operator... : ... ? ...
(see https://www.tutorialspoint.com/Java-Ternary-Operator-Examples, if you are not familiar with it). Explain what this operator achieves in this particular function and rewrite the function usingif ... else ...
statement instead of the operator. - There are several classes inside the
ArrayList<E>
class. These are called inner classes. Locate the one calledListItr
. Study it with your group. Try to explain what the purpose of that class is (feel free to use the code as well as the documentation). Point out things in the code of that class that seem strange, or that you do not understand.
Part 3
(There are no Gradescope questions for this part, but the answers are important to understand.)
-
Look at the documentation for some of the public methods in the source code and compare it to the documentation on the website. What do you notice?
- Still looking at the documentation explain what the following keywords seem to do:
@param
@return
@throws
-
The
sort
function defined in theArrayList<E>
class takes an object of typeComparator<? super E>
as a parameter. Research what this type represents. In what ways is it similar and in what ways is it different than theComparable<E>
interface? - Study the
equals
method in theArrayList<E>
class. Try to figure out step by step how it works. You will need to look at other methods that it calls to have a full understanding.