Chapter 7 discusses the ArrayList class, which allows dynamic storage of objects, automatically expanding and shrinking as items are added or removed. It covers how to create an ArrayList, add and access items, iterate over them, and modify the list using methods like add, remove, and set. Additionally, it explains the default capacity of an ArrayList and how to create one that holds specific object types.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views11 pages
Array Lists Guide
Chapter 7 discusses the ArrayList class, which allows dynamic storage of objects, automatically expanding and shrinking as items are added or removed. It covers how to create an ArrayList, add and access items, iterate over them, and modify the list using methods like add, remove, and set. Additionally, it explains the default capacity of an ArrayList and how to create one that holds specific object types.
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 java.util.ArrayList;
Creating an ArrayList ArrayList<String> nameList = new ArrayList<String>();
• Notice the word String written inside angled
brackets <> • This specifies that the ArrayList can hold String objects. • If we try to store any other type of object in this ArrayList, an error will occur.
Creating and Using an ArrayList • You can use the enhanced for loop to iterate over each item in an ArrayList. // Create an ArrayList of names. ArrayList<String> nameList = new ArrayList<String>(); nameList.add("James"); nameList.add("Catherine"); nameList.add("Bill");
// Display the items in the ArrayList.
for (String name : nameList) System.out.println(name);
Using an ArrayList • The ArrayList class's toString method returns a string representing all items in the ArrayList System.out.println(nameList); This statement yields : [ James, Catherine ] • The ArrayList class's remove method removes designated item from the ArrayList nameList.remove(1); This statement removes the second item. • See example: ArrayListDemo3.java
Using an ArrayList • 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: nameList.add(1, "Mary"); This statement inserts the String "Mary" at index 1 • To replace an existing item, use the set method: nameList.set(1, "Becky"); This statement replaces “Mary” with “Becky” • See example: ArrayListDemo4.java
Using an ArrayList • An ArrayList has a capacity, which is the number of items it can hold without increasing its size. • The default capacity of an ArrayList is 10 items. • To designate a different capacity, use a parameterized constructor: ArrayList<String> list = new ArrayList<String>(100);
Using an ArrayList // Create a listor to hold InventoryItem objects. ArrayList< InventoryItem> list = new ArrayList< InventoryItem>(); InventoryItem invObj
// Add three InventoryItem objects to the ArrayList.
invObj = new InventoryItem("Nuts", 100); list.add(invObj); invObj = new InventoryItem("Bolts", 150); list.add(invObj) invObj = new InventoryItem("Washers", 75); list.add(invObj);
// Display each item.
for (int index = 0; index < list.size(); index++) { InventoryItem item = list.get(index); System.out.println("Item at index " + index + "\nDescription: " + item.getDescription() + "\nUnits: " + item.getUnits()); } See: ArrayListDemo6.java