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
Chapter Arrays and the ArrayList Class The ArrayList Class Contents 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 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 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 The next item that is added to 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 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] IV Removing an Item from an ArrayList The ArrayList class has a remove method that removes an item at a specific index V Inserting an Item 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”); 10 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”); 11 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); 12 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); 13 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 name = new ArrayList(); 14 15 16 ... a Cast Operator with the get Method IX Using ArrayList As a Generic Data Type I The ArrayList Class The ArrayList class can be used for storing and retrieving objects An ArrayList object is... argument to the method 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. .. 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