To write an ArrayList version of the program, we will have to introduce
several new Java concepts, including the class
ArrayList. We will also
review different kinds of
for loops used in Java.
Before going any further, we suggest you run the
program in IntelliJ. You can view this program in the
Once you’ve done that, let’s look at what is happening in the Java source code.
Here we declare and initialize two objects,
which appear to be of type
ArrayList<Double>, respectively. An
ArrayList in Java is very
similar to an Array. Like an
Array, we must let
the compiler know what kind of objects our
ArrayList is going to
contain. In the case of
ArrayList will contain
values of type
String (representing the names of the students), so we use the
ArrayList<String> syntax to inform the compiler that we intend to
fill our list with Strings. Similarly,
grades will hold exclusively
values of type
Double and is declared to be of type
Notice that we declared
grades to be of type
using the wrapper class
Double rather than the primitive type
double. All values stored in Java collections must be objects, so
we’ll have to use object types in those situations.
In lines 10 and 11, we also initialize each list by creating a new, empty
list. Note that when we call the
ArrayList constructor, as in
new ArrayList<>(), we don’t need to specify type (it’s implicit in the
left-hand side of the assignment).
You will sometimes see the
ArrayList class written as ArrayList,
E represents a placeholder for the type that a programmer will
declare a given list to hold. This is especially true in documentation.
You can think of
E as representing an arbitrary type.
Classes like ArrayList that take another type or class as a parameter are referred to as generic classes or generic types.
We then use a
do-while loop to collect the names of each of the students
in the class.
Recall that a
do-while loop is very similar to a
while loop, but the
execution condition is checked at the end of the loop block. This has the net
effect that the code block will always run at least once. In this example, we
prompt the user for a name, which Java processes via
the user hits the enter key. To finish entering names, the user enters a blank
For each student that is entered (that is, each non-empty line), we add
String to the end of our list with
.add() method is provided by the
There are lots of other ArrayList methods to get familiar with, some of which
we will discuss in more detail below.
Note that our program imports
java.util.ArrayList to take advantage of this
Java provided class.
do-while loop are two different loops that demonstrate two ways
you can loop through a list in Java. Here’s the first, which collects the
numeric grade for each student:
This, you may recall, is Java’s
for-each loop syntax. You may read this
in your head, or even aloud, as:
for each student in students. As you might
expect at this point, we must declare the iterator variable
with its data type.
The next loop on display prints out each student’s name and grade:
Here, we introduce the syntax
students.size() which utilizes the
ArrayList. This method returns the integer representing the
number of items in the list. This is similar to String’s
for loop, we use a loop index to define the starting point,
ending point, and increment for iteration. It may be helpful for you to
consider this kind of construction as something like,
for integer i in the range of the number of items in students.... The first statement inside the
parenthesis declares and initializes a loop index variable
i. The second
statement is a Boolean expression that is our exit condition. In other words,
we will keep looping as long as this expression evaluates to
third statement is used to increment the value of the loop index variable at
the end of iteration through the loop.
Again, the syntax
i++ is Java shorthand for
i = i + 1. Java also
supports the shorthand
i-- to decrement the value of
We can also write
i += 2 as shorthand for
i = i + 2.
In the final lines of the program, we compute the average grade for all students:
Let’s gather up a few of the
ArrayList methods that we’ve encountered so
far, along with a few new ones. While these will be the most common methods and
properties that you use with this class, they by no means represent a complete
list. Refer to the
official documentation on the ArrayList
for such a list, and for more details.
To demonstrate the use of these methods, we’ll create a new
ArrayList<String> planets = new ArrayList<>();
Ok, we’ve got an empty ArrayList. We need to use the class’s
to populate this collection with items.
There are other means to declare and initialize an ArrayList in fewer lines.
These require knowledge of other collections types, so we’ll stick with
for the time being.
.add() to populate
Thus, the first item in this table:
|Adds an item to the ArrayList|
|Returns the number of items in an ArrayList, as an |
|Checks to see if the ArrayList contains a given item, returning a Boolean|
|Looks for an item in an ArrayList, returns the index of the first occurrence of the item if it exists, returns -1 otherwise|
Here’s a couple more methods that require slightly longer descriptions:
|Rearranges the elements of a |
This method is technically used on Java’s
Collections class and
thus requires a different
Collections is itself a member of the collections framework but not all
members of the framework are instances of this class. We include this method
here because, should you be in the market for a sorting method, this is a
helpful one to know.
|Returns an Array containing the elements of the ArrayList|
Perhaps you recall that in Java, you must know the size of the Array when you
create it. So we’ll need to first define the new Array before we can use
Speaking of Arrays, let’s see the Array version of Gradebook next.
Check Your Understanding
The number of entries in an
ArrayList may not be modified.
ArrayList<String> charStars = new ArrayList<>(); charStars.add('a'); charStars.add('b'); charStars.add('c');
ArrayList<Char> charStars = new ArrayList<>(); charStars.add('a'); charStars.add('b'); charStars.add('c');
ArrayList<char> charStars = new ArrayList<char>('a', 'b', 'c');
ArrayList<String> charStars = new ArrayList<>(); charStars.add("a"); charStars.add("b"); charStars.add("c");