CS1331: Introduction to OOP
Georgia Institute of Technology
ArrayList
Dr. Aibek Musaev
1
Material Covered
ArrayList
◼ Generics
◼ Comparable interface
◼ Practice!
◼
2
ArrayList (sec 11.11-11.12)
◼
◼
◼
◼
An ArrayList is a combination of an Array (fixed size, same data type) and a data structure known as a
List (which can change in size, but still contains the same data type)
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html
import java.util.ArrayList; // to use Java's ArrayList class
The following code will create an ArrayList with an initial capacity of 20 that holds Strings
ArrayList<String> list = new ArrayList<String>(20);
list.add(“Jane”); // position 0
String s = new String(“Mary”);
list.add(s); // position 1, adds to the end of the list
list.add(1, “Lexi”); // adds “Lexi” at index 1
/* sets element 0 to “Paula”
* returns element previously specified position
* can only set from 0… n-1
*/
s = list.set(0, “Paula”);
list.add(s);
s = list.remove(1); // returns removed element
s = list.get(2); // returns element at index of 2
3
ArrayList (sec 11.11-11.12)
◼
Some other useful ArrayList methods are:
boolean isEmpty()
❑ int size()
❑ boolean contains(Object)
❑ int indexOf(Object)
❑
ArrayList is implemented using (i.e., backed by) an
array with the dynamic resizing occurring "behind
the scenes"
◼ This means an ArrayList is NOT the most efficient
of the collections Java provides, so don't use this if
you only need an array of fixed size.
◼ Demo
◼
4
Generics (chapter 19 -up to using generic classes)
◼
Generics – the use of parameterized types in
class and method definitions.
We can write a generic class or method using
generics so that it can handle any different type of
object
❑ Generics allow us to parameterize the types of
variables/fields
❑ Generics were introduced in Java 5
❑
◼
ArrayList is an example of a Generic class, as
the type of data the ArrayList stores is
parameterized
5
Generics (chapter 19 -up to using generic classes)
◼ Generic ArrayList in JDK 1.5
6
Generics (chapter 19 -up to using generic classes)
Originally ArrayList stored objects, which would
provide no real compile-time checking that we are
using it correctly (i.e., storing the same data types in
it).
◼ As of Java 5, we can parameterize the data type
stored within ArrayList and the methods will "adjust"
to that type, granting us additional compile-time
checks for type-safety
◼ This means that the key benefit of generics is to
enable errors to be detected at compile time
rather than at runtime.
◼
ArrayList<what goes here?>
// what type of objects should the ArrayList store/work with
7
Using Generics
◼ Comparable revisited.
What if we could know more details about what
we are comparing?
❑ If we look closely at the API, we'll see
Comparable is parameterized
❑
◼
◼
◼
❑
Comparable<T>
int compareTo(T o)
T is the placeholder for the type
Let's make GrizzlyBear support the
parameterized Comparable interface
◼
See GrizzlyBear.java
8
Using Generics
◼ Generic type
◼ The new generic type detects possible errors
at compile time.
Runtime error
Compilation error
9
Using Generics
We can also create an ArrayList of GrizzlyBear
ArrayList<GrizzlyBear> bears = new ArrayList<GrizzlyBear>();
◼
Alternatively, we can leave out the second GrizzlyBear in the instantiation.
Java will be "smart" and infer the parameterized type from reference
variable declaration
ArrayList<GrizzlyBear> bears = new ArrayList<>();
◼
The following code will produce a compile error because we are trying to put
a String into an ArrayList of GrizzlyBear
bears.add("Jane"); // compile error because ArrayList<GrizzlyBear>
◼
We can also iterate over the collection of GrizzlyBear using a for-each loop
for (GrizzlyBear gb : bears) System.out.println(gb);
◼
10
Using Generics
◼
Although the generic parameters can be words, it's
convention to label them as a single uppercase letter
❑
❑
❑
❑
❑
❑
E → Element
K → Key
N → Number
T → Type
V → Value
S, U, V, etc. 3rd, 4th, 5th etc. types
11
Practice!
Do you know the names of the Beatles members? They
were George, Ringo, John, and Paul, in no particular order.
◼ Do you know their ages when they started the band? They
were 20, 23, 23, and 21, respectively.
◼ Define a Person class that implements the Comparable
interface, which defines an order based on their age.
◼ Then create an ArrayList of Person objects, add the four
Beatles members and sort it using the Collections.sort()
method, which uses the ordering defined by the
compareTo() method to sort the elements.
◼ Loop over the sorted ArrayList to print the name and age of
each person in the list.
◼ Update the compareTo() method to include the name in the
ordering if ages match.
◼
12
Review
◼ Shadowing
◼ Overriding/overloading
◼ Static vs instance
◼ Static vs dynamic
◼ Super chaining
13