O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7
The ArrayList Class and the enum Keyword
7.1 Introduction to the ArrayList Class ................................................... 137
7.2 Immutable Objects ..................................................................... 138
7.3 The StringBuffer Class ................................................................ 140
7.4 The Interface of an ArrayList ......................................................... 142
7.5 Introducing the enum Construct ........................................................ 144
7.6 Summary ................................................................................ 152
7.7 Syntax .................................................................................. 152
7.8 Important Points ....................................................................... 153
7.9 Exercises ................................................................................ 153
7.10 Lab ...................................................................................... 154
7.11 Project .................................................................................. 154
The main goal of the chapter is to introduce the reader to the ArrayList class. It allows
us to create an array without specifying the size of the array. Creating an array this way is
much more convenient because the amount of main memory that is needed by the array is
automatically managed. The chapter also covers the topic of immutable objects. These are
objects that cannot be modified. Lastly, the chapter focuses on the enum construct. Unlike
a class that can merge several primitive types, an enum is used to define a type that is
restricted to a finite subset of a primitive type.
7.1 Introduction to the ArrayList Class
One limitation of arrays is that their size cannot be changed once fixed. Consider an
application that stores information about employees. Suppose that initially we do not know
the number of employees that we want to store. Maybe we can create an array of one
thousand employees? This approach has two drawbacks. First, a lot of space is allocated
that may never be used. Second, if we end up using more than one thousand employees,
then the program will crash. Therefore, a more versatile approach for managing the size of
an array is needed. The ArrayList class fulfills this task. An ArrayList of employees can
be defined as follows.
ArrayList<Employee> emps = new ArrayList<Employee> () ;
The above definition states that the variable emps refers to an array of employees. However,
it does not specify the size of the array. Note that an ArrayList is not your normal class.
It is a generic class.
A generic class takes as input the name of one or more classes as a parameter.
These classes are specified immediately after the name of the class, they are separated
by commas, and they are surrounded by angle brackets <>.
137
138 Learning Java through Games
In our example, the ArrayList class takes as input the Employee class. This means that
the array list can store inside it only employees. If we try to insert an object of a different
type in the ArrayList, then an error will occur. Note that, starting with Java 7, the above
syntax can be simplified as follows.
ArrayList<Employee> emps = new ArrayList <>() ;
The second reference to the Employee class is redundant, and therefore can be omitted.
Note that a generic class can take as input only the name of a class and not the name of a
primitive type. For example, the following code will not compile.
ArrayList<int> dice = new ArrayList<int >() ;
However, we can use the wrapper Integer class as input to the ArrayList class. For
the most part, an Integer object behaves exactly like an int, but it is still an object.
Therefore, an ArrayList of integers can be declared as shown below.
ArrayList<Integer> dice = new ArrayList <>() ;
A common mistake by novice programmers is to specify the size of the ArrayList as
a parameter in the parentheses. However, this is the wrong approach because we do not
need to specify the size of an ArrayList when we create it. The size of an ArrayList is
dynamically determined by the number of objects that is stores. Therefore, a newly created
ArrayList will always have size of zero.
7.2 Immutable Objects
Consider the following code.
class Test{
public s ta tic void inc ( Integer i ){
i ++;
}
public s ta tic void main( String [ ] args ){
Integer i=3;
inc ( i ) ;
System.out.println(i);
}
}
The behavior of the code is very interesting and counterintuitive. Note that the code:
Integer i = 3 is rewritten by Java to: Integer i = new Integer(3). Therefore, as Fig-
ure 7.1 suggests, a new object is created inside the main method. Let the location of the
new object be 2000. Next, this object is passed as a parameter to the inc method. Recall
that only the object’s address will be passed to the inc method. Now the variable i inside
the inc method will be initially equal to 2000. However, if we try to display the variable,
then we will see the number 3. The reason is that the Integer class contains a toString
method that returns the integer that is stored inside the class.
Next, let us examine the statement i++. This is where all the magic happens! This code
is equivalent to i=i+1, which in turn means change the value inside the i object to 4.
However, the Integer classisimmutable.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required