First Look at
the Collections Framework: The ArrayList class
- ArrayList class in java.util package
- Ordered collection of objects that is more flexible
than an array
- An ArrayList object does not have a fixed size, so
the size of the list is not set
- ArrayList class contains methods get(), add(), size(),
remove() that make it easy to carry out common tasks on a list
The ArrayList class is a generic
class:
a list of type ArrayList<T> is a collection of objects of type T.
Example:
ArrayList<Circle>
circles = new ArrayList<Circle>();
circles.add(new
Circle(1)); // add circle with radius 1 to the list
circles.add(new
Circle(2));
Note: There are brackets around
the type Circle because this is a type parameter. You can replace
"Circle" with any class, so we call ArrayList a generic class. We
cannot replace "Circle" with a primitive type, so we cannot create an ArrayList of ints
with ArrayList<int>.
ArrayList Methods
The size() method
When an ArrayList is first created, it has size 0. To get the size of
an ArrayList, call the size() method:
int listSize =
circles.size(); // listSize is 2
The get() method
To get an object at some specified position from an ArrayList, use the
get() method. The argument for get() specifies the index of the element
you want, where indexing into an ArrayList starts at 0:
Circle
myFavoriteCircle = circles.get(1); // this gets the second circle in
the list
Note: There is no element at
index circles.size() - the following line will cause an out of bounds
exception: myFavoriteCircle = circles.get(circles.size()); // exception
thrown
The set() method
To assign or set a new value to an existing
array list element, use the set() method.
Circle cir =
new Circle(10); // create new Circle with radius 10
circles.set(1,
cir); // Element at position 1 of circles array list is now the new
circle
The
add() method
We can use add() to add a new element to the end of an array list, or
to add a new element at a specified position in the list. This method
does not overwrite list elements - if we add an element at position i,
the elements from position i on are moved down one position in the
list.
circles.add(new
Circle(4)); // add circle of radius 4 to end of array list (in position
2)
System.out.println("circle
list size: " + circles.size()); // Output: 3
circles.add(1,
new Circle(20)); // add new circle of radius 20 at position 1 in the
list
System.out.println("circle
list size: " + circles.size()); // Output: 4
The remove() method
To remove the array list element at a specified position, use the
remove() method.
circles.remove(3);
// removes the circle of radius 4 at position 3 (the end of the list)
Array Lists and Primitive Types
Array lists cannot contain elements of a
primitive type. Fortunately, each primitive type has what is called a Wrapper Class.
- Wrapper class for int: Integer
- Wrapper class for double: Double
- Wrapper class for char: Character
- Wrapper class for boolean: Boolean
A wrapper class instance contains a value of the
corresponding primitive type. The primitive type value is passed to the
constructor of the wrapper class:
Integer intWrap
= new Integer(14);
or, alternatively,
Integer intWrap
= 14; // short-cut version of above - called "auto-boxing", only
available with Java 5 and 6
It is also easy to convert
the wrapper class object back to the primitive type:
int myInt =
intWrap; // sets myInt to 14, called "auto-unboxing"... before Java 5,
we used the intValue method:
int myInt = intWrap.intValue(); // needed before Java 5
Auto-boxing and auto-unboxing make it
easy to perform operations on wrapper class objects:
intWrap =
intWrap + 1; // intWrap now contains the integer 15
Array List Examples
Example: Write Java code that
creates an array list of 10 strings. Then print the strings in the
list.
Example: Create an array list
of type Double. Add 3.14, 2.71, and 15 to it. Then add 0 at the
beginning of the list.
The for each Loop
This new loop structure makes it easy to iterate through all elements
in a list or array. It only allows us to traverse the entire array,
starting from the first entry.
Example:
ArrayList<Square>
sqrs = new ArrayList<Square>();
sqrs.add(new
Square(1)); //add square with side length of 1
sqrs.add(new
Square(2));
sqrs.add(new
Square(3));
// loop through
the list and print the area of each square
for(Square s:
sqrs) // for each s in sqrs - s takes on each Square object in the
list, one per iteration
{
System.out.println("Area: " + s.area()); // print area of current square
}
Example:
int[] nums =
{1, 2, 3, 4, 5, 6};
int sum = 0;
// compute sum
of array elements using a for each loop
for(int i :
nums)
sum = sum + i;