Chapter 8 Chapter 8 Arrays and the ArrayList Class The ArrayList Class 2 Contents I The ArrayList Class II Creating and Using an ArrayList Object III The ArrayList Classs toString method IV Removing. ngôn ngữ java lập trình java học lập trình tai lieu java lập trình code
Trang 1Chapter 8
Arrays and the
ArrayList Class
The ArrayList Class
Trang 2Contents
I The ArrayList Class
II Creating and Using an ArrayList Object
III The ArrayList Class's toString method
IV Removing an Item from an ArrayList
V Inserting an Item
VI Replacing an Item
VII Capacity
VIII Using a Cast Operator with the get Method
IX Using ArrayList As a Generic Data Type
Trang 3I The ArrayList Class
The ArrayList class can be used for storing
and retrieving objects
An ArrayList object is similar to an array of
objects, but offers many advantages over an
array:
An ArrayList object automatically expands as items are added to it
In addition to adding items to an ArrayList, we can remove items as well
An ArrayList object automatically shrinks as
items are removed from it
Trang 4II Creating and Using an
ArrayList Object
Creating an ArrayList object
ArrayList nameList = new ArrayList();
Adding items to the ArrayList object
nameList.add(“James”);
nameList.add(“Catherine”);
nameList.add(“Bill”);
The items that are stored in an ArrayList have
a corresponding index
The first item that is added to an ArrayList is stored at index 0 The next item that is added to
an ArrayList is stored at index 1 and so forth
Trang 5II Creating and Using an
ArrayList Object
To get the number of items stored in an
ArrayList: using the size() method
The ArrayList class's get method returns the item stored at a specific index
We pass the index as an argument to the method
Trang 66
Trang 7III The ArrayList Class's
toString method
The ArrayList class has a toString method that returns a string representing all of the items stored in an ArrayList object
ArrayList nameList = new ArrayList();
nameList.add(“James”);
nameList.add(“Catherine”);
nameList.add(“Bill”);
System.out.println(nameList);
[James, Catherine, Bill]
Trang 8IV Removing an Item from an
ArrayList
The ArrayList class has a remove method
that removes an item at a specific index
Trang 9V Inserting an Item
Trang 10V Inserting an Item
The add method adds an item at the last position
in an ArrayList object
nameList.add(“Marry”);
The ArrayList class has an overloaded version
of the add method that allows to add an item at a specific index
nameList.add(1, “Marry”);
Trang 11VI Replacing an Item
The ArrayList class's set method can be used
to replace an item at a specific index with another item
nameList.set(1, “Becky”);
Trang 12VII Capacity
An ArrayList object has a capacity, which is
the number of items it can store without having to increase its size
When an ArrayList object is created, using the no-argument constructor, it has an initial capacity
of 10 items
We can specify a different starting by passing an int value to the constructor:
ArrayList nameList = new ArrayList(100);
Trang 13VIII Using a Cast Operator with
the get Method
The get method returns a reference to the object stored at a specific index
We have to use a cast operator to convert the
reference to the correct type manually
ArrayList nameList = new ArrayList();
nameList.add(“James”);
String str = (String)nameList.get(0);
Trang 14IX Using ArrayList As a
Generic Data Type
We can specify the type of object that an
ArrayList will store, and Java will make sure
that only objects of the specified type are stored in it
ArrayList<String> name = new ArrayList<String>();
Trang 1515