Lesson 13
ArrayList
Author: Brandon Fajardo (11/10/17) Updated by Gillian VerSteeg (9/13/20)
Introduction
Review: In the last lesson, we talked about arrays.
Lesson topic: In this lesson, we will be going over ArrayLists, which are a different way to store a collection of values.
Topic 1 - ArrayList
Subtopic 1 - Definition
ArrayList: An array of objects.
An ArrayList is very similar to an array but solves many of the common problems with arrays, including:
An ArrayList automatically resizes itself when necessary
An ArrayList lets you insert objects into the middle of the collection
An ArrayList lets you delete items
A difference between arrays and ArrayLists is how ArrayLists do not use primitive data types such as int, double, char, and long. What they can hold are Strings (because they are an object) and Wrapper Classes.
Wrapper Class: A Java class that was created to hold one primitive data type. Examples are Integer, Double, Byte, and Short. Objects of these types hold one value of their corresponding primitive type (int, double, byte, short). They are used when you desire to store primitive data types in Java structures that require objects (ex: JLists, ArrayLists, JComboboxes, JTables).
Note:
Wrapper Classes are not as efficient as primitive types
Arrays have objects, while ArrayLists have objects.
Subtopic 2 - Declaring
To use an ArrayList, you must first import the ArrayList Class.
To declare an ArrayList, you can use either of the following two notations:
Note: When declaring an ArrayList, you do not need to put a data type within the second <>.
Topic 2 - Methods
After declaring an ArrayList, then you can use the following methods:
Subtopic 1 - add() and get()
Add() is used to add an object to the ArrayList.
Example of add():
To add objects in an ArrayList, you use the following notation:
Ex:
As you can see, this assigns a new object to the index number 2, where "Ford" used to be. This does not, however, delete the object in that index, it just shifts over. Now there are five objects in the ArrayList "cars" rather than four.
Just like adding an object in an array, you also use the index number for get().
From this example, we can see that get() was used to print only one object of the ArrayList. Get() can also be used in other settings such as mathematical equations.
Note: Remember, like arrays, ArrayLists also use indexes. Indexes always start at 0, then continue in increments of 1.
Challenge #1: How do you create a "Double" ArrayList called "numbers" with five objects in this order:
17.4
0.12
98.001
34.7
8
Then use get() to print 3 things:
Multiply the second number by the third,
Add the fifth number to the fires then divide by 2.
Get the modulus of the fourth number divided by the second.
Answer:
Subtopic 2 - set()
set() is used to modify an object by referring to its index number.
Notation:
Example:
Subtopic 3 - remove()
When you remove an index, it does not become empty. Rather, all of the indexes after it move down a number.
There are two ways to remove an index:
The only way the second notation works is if the "object value" is exactly the same. Otherwise, nothing will be removed.
Example:
Subtopic 4 - size() and clear()
size() is used to find the size of an ArrayList.
Notation:
Example:
Note: When using size(), the variable you will receive does NOT count indexes. Rather, it counts the number of objects.
clear() is used to remove all objects of an ArrayList.
Even though you clear an array, it is still there. If you want to use it later, you do not need to rename it.
Topic 3 - Printing ArrayLists
Subtopic 1 - Using a Loop
There are different ways to print ArrayLists besides using "System.out.println." One of these ways is to use a loop. When using a loop, the output will look like a list of variables, with one object per line.
Ex:
Subtopic 2 - Using an Iterator
Iterator: Spits out a collection's values, one after another.
To obtain values from a collection using an iterator, you must use next().
To find out whether the iterator has any more values, you use hasNext().
When using an Iterator, this allows a person to print out each of the objects in one continuous line rather than separated by commas within [ ].
Ex:
Note:
Remember to import java.util.Iterator;
To use an iterator correctly, remember to use System.out.print instead of System.out.println.
Topic 4 - The "Collections" Class
The Collections class, which uses "import java.util.Collections;" sorts an ArrayList either alphabetically or numerically, depending on the data type. It uses the sort() method.
Ex:
reverseOrder() is used to print a list in reverse order. It works just like sort(), but in reverse.
Ex:
Note: When you sort an ArrayList, it does not return to the original index.
Topic 5 - Copying ArrayLists
Shallow Copy: Only the object references are copied. Uses clone(). Deep Copy: Creates an exact copy of all the fields and references.
Simple Example of Copying:
A "Deep" copy is much more complicated than a Shallow copy. For more information about copying ArrayLists, please see https://howtodoinjava.com/java/collections/arraylist/arraylist-clone-deep-copy/.
Conclusion
Recap: In this lesson, we covered ArrayLists and their methods. We also examined different ways to print ArrayLists and to copy them.
Next: This was the last lesson! Congratulations! Read the next note for some final statements
Last updated