0% 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.
Copyright
© © All Rights Reserved
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% 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.
Copyright
© © All Rights Reserved
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
You are on page 1/ 11

CHAPTER 7

Arrays and the


ArrayList
Class

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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;

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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.

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


Using an ArrayList

• To populate the ArrayList, use the add


method:
• nameList.add("James");
• nameList.add("Catherine");

• To get the current size, call the size method


• nameList.size(); // returns 2

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


Creating and Using an ArrayList

• To access items in an ArrayList, use the


get method
nameList.get(1);

In this statement 1 is the index of the item to get.

• Example: ArrayListDemo1.java

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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);

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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);

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


Using an ArrayList
• You can store any type of object in an
ArrayList
ArrayList<InventoryItem> accountList =
new ArrayList<InventoryItem>();

This creates an ArrayList that can hold


InventoryItem objects.

Copyright © 2018 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2018 Pearson Education, Inc. All Rights Reserved

You might also like