13
Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Chapter 16: The ArrayList class

Chapter 16: The ArrayList class

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 16: The ArrayList class

Think Java:

How to Think Like a Computer

Scientist

5.1.2

by Allen B. Downey

Chapter 16:

The ArrayList class

Page 2: Chapter 16: The ArrayList class

8-2

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;

Page 3: Chapter 16: The ArrayList class

Creating an ArrayList

8-3

ArrayList<String> nameList =

new ArrayList<String>();

This ArrayList only holds String objects

Add any other type of object in this ArrayList,

and an error will occur.

Page 4: Chapter 16: The ArrayList class

8-4

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

Page 5: Chapter 16: The ArrayList class

Memory Map for the ArrayList<String> nameList

1-5

Page 6: Chapter 16: The ArrayList class

8-6

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. (Catherine)

• Example: ArrayListDemos.java, Demo 1

Page 7: Chapter 16: The ArrayList class

ArrayList Traversals (two ways)

1. A for loop, similar to Array traversals

2. A special syntax for traversals (for-each loop)

"for each name in nameList, print the name"

• Example ArrayListDemos.java, Demo 2 1-7

Page 8: Chapter 16: The ArrayList class

8-8

Displaying an ArrayList (toString)

• The ArrayList class's toString method returns a string representing all items in the

ArrayList

System.out.println(nameList);

This statement yields : [ James, Catherine ]

Page 9: Chapter 16: The ArrayList class

8-9

Deleting items (remove)

• The ArrayList class's remove method removes designated item from the ArrayList

nameList.remove(1);

This statement removes the second item.

• See example: ArrayListDemos.java, Demo 3

Page 10: Chapter 16: The ArrayList class

8-10

Inserting items (add)

• 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

Page 11: Chapter 16: The ArrayList class

8-11

Replacing items (set)

• To replace an existing item, use the set method:

nameList.set(1, "Becky");

This statement replaces “Mary” with “Becky”

• See example: ArrayListDemos.java, Demo 5

Page 12: Chapter 16: The ArrayList class

ArrayList storage

• You can store any type of object in an ArrayList

8-12

ArrayList<Point> curve =

new ArrayList<Point>();

This creates an ArrayList that can hold Point objects.

See example: ArrayListDemos.java, Demo 6

Page 13: Chapter 16: The ArrayList class

Continue Demo:

• Demo DragonCurve

• Lab 16

– Modify BattleBug to act like BlueBug

– http://greenteapress.com/thinkapjava/html/thinkjava018.html#toc154

– Mod BugRunnerWithKeystroke to work like snake game

1-13