Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and the ArrayList Class The ArrayList Class

Similar presentations


Presentation on theme: "Arrays and the ArrayList Class The ArrayList Class"— Presentation transcript:

1 Arrays and the ArrayList Class The ArrayList Class
Chapter 8 Arrays and the ArrayList Class The ArrayList Class

2 Contents The ArrayList Class Creating and Using an ArrayList Object
The ArrayList Class's toString method Removing an Item from an ArrayList Inserting an Item Replacing an Item Capacity Using a Cast Operator with the get Method Using ArrayList As a Generic Data Type

3 I. 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.

4 II. 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.

5 II. 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.

6

7 III. 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]

8 IV. Removing an Item from an ArrayList
The ArrayList class has a remove method that removes an item at a specific index.

9 V. Inserting an Item

10 V. 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”);

11 VI. 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”);

12 ArrayList nameList = new ArrayList(100);
VII. 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);

13 VIII. 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);

14 IX. 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>();

15

16


Download ppt "Arrays and the ArrayList Class The ArrayList Class"

Similar presentations


Ads by Google