ArrayList
Lecture 17
Based on Slides of Dr. Norazah Yusof
The ArrayList Class
Similar to an array, an ArrayList allows
object storage
Unlike an array, an ArrayList object:
Automatically expands when a new item is
added
Automatically shrinks when items are removed
Requires:
import [Link];
8-2
Creating and Using an ArrayList
and adding items using add() method
Create an ArrayList object with no-args
constructor
ArrayList townList = new ArrayList();
To add element to the ArrayList, use the
add method:
[Link]("Kangar");
[Link]("Alor Setar");
To get the current size, call the size method
[Link]();
// returns 2
Example: Lab 6 Exercise 1 Question 3
8-3
Accessing & Removing items in an
ArrayList
To access items in an ArrayList, use the get method
as follows:
[Link](1); // 1 is the index of the item to get.
A loop is used in the following statement to access every
element in the ArrayList named townList.
for(int i=0;i<[Link]();i++)
[Link]([Link](i)+" ");
To remove items in an ArrayList, use the remove
method
[Link](1); //This statement removes the second item.
[Link]("Penang"); //This statement removes the item
// with the value "Penang".
8-4
Adding and replacing existing items using
two argument method
The ArrayList class's add method with one
argument adds new items to the end of the
ArrayList
To insert items at a location of choice, use the
add method with two arguments:
[Link](6, "Shah Alam");
This statement inserts the String "Shah Alam" at index 6
To replace an existing item, use the set
method:
[Link](1, "Muar");
This statement replaces the value at index 1 with Muar
8-5
Using toString() method
The ArrayList class's toString
method returns a string
representing all items in the
ArrayList
[Link](townList);
This statement yields :
[Muar, Alor Setar]
8-6
Using a Cast Operator with the get
Method
An ArrayList object is not typed
To retrieve items from an ArrayList,
you must cast the item to the appropriate
type
ArrayList nameList = new ArrayList();
[Link]("Kluang"); // Inserts an item
String str = (String)[Link](0);
Try get without the cast to see the effect.
8-8
Using ArrayList as a Generic Data Type
You can create a type-safe ArrayList
object by using generics.
For example an ArrayList object for
Strings:
ArrayList<String> nameList = new
ArrayList<String>();
The get method no longer requires
casts to work.
8-9
ArrayList Methods
add(Object o) add new object into ArrayList
get(int index) - retrieves object reference from
ArrayList index position
size() - returns ArrayList size
remove(int index) - removes the element at the
specified position in this list. Shifts any
subsequent elements to the left and returns the
element that was removed from the list.
indexOf(Object o) - finds the index in this list of
the first occurrence of the specified element
clear() - removes all of the elements
Exercises
Do Exercise 1:
Question
1, page 115
Question 2, page 116
Question 3, page 117
Question 1, page 115
import [Link];
public class CircleArrayList
{
private double radius;
public CircleArrayList(double radius) {
[Link]=radius;
}
public void setRadius() {
[Link]=radius;
public double getRadius() {
return radius;
public double getArea() {
return ([Link]*radius*radius);
}
Question 1, page 115
import [Link];
public class TestCircleAL {
public static void main(String[] arg) {
ArrayList cList = new ArrayList();
[Link](new CircleArrayList(2.3));
[Link](new CircleArrayList(3.3));
[Link]("The area : %2.2f "+
((CircleArrayList)[Link](0)).getArea());
}
}
Question 2, page 116
import [Link];
ArrayList student = new ArrayList();
[Link](Siti Rahimah);
[Link](Robert Lau);
[Link](student);
[Link](1, Muhammad);