Chapter 13 - java.util.ArrayList. In this chapter, the learning objectives are: Learn about java.util.List interface; learn about the java.util.ArrayList class, its constructors and methods; review some of the ArrayList pitfalls; practice with Part 4 of GridWorld ― “Critters”.
Trang 1and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
Chapter13
size capacity
Trang 2Objectives:
• Learn about java.util.List interface
• Learn about the java.util.ArrayList class, its constructors and methods
• Review some of the ArrayList pitfalls
• Practice with Part 4 of GridWorld ― “Critters”
Trang 3java.util.ArrayList<E>
• Implements a list using an array
• Implements java.util.List<E> interface
«interface»
java.util.List
java.util.ArrayList java.util.LinkedList
Trang 4java.util.ArrayList<E> cont’d
• Implements a list using an array
• Can only hold objects (of a specified type), not elements of primitive data types
• Keeps track of the list capacity (the length of the allocated array) and list size (the number
of elements currently in the list)
"Cat" "Hat" "Bat"
capacity size
Trang 5
ArrayList Generics
• Starting with Java 5, ArrayList and other
collection classes hold objects of a specified data type
• The elements’ data type is shown in angle brackets and becomes part of the List and
ArrayList type For example:
ArrayList<String> words = new ArrayList<String>();
List<Integer> nums = new ArrayList<Integer>();
Trang 6default capacity (ten)
Java docs use the letter E as
the type parameter for elements
in generic collections
Creates an empty
ArrayList<E> of the
specified capacity
Trang 7boolean add (E obj)
void add (int i, E obj)
E set(int i, E obj)
E get(int i)
E remove(int i)
boolean contains(E obj)
int indexOf(E obj)
both use equals to
compare objects
i must be from 0 to size() -1
inserts obj as the
i-th value; i must
be from 0 to size() returns true
Trang 9ArrayList<E> Details
• Automatically increases (doubles) the capacity when the list runs out of space (allocates a
bigger array and copies all the values into it)
• get(i) and set(i, obj) are efficient because an array provides random access to its elements
• Throws IndexOutOfBoundsException when
i < 0 or i size()
(or i > size() in add (i, obj) )
Trang 10ArrayList<E> Autoboxing
• If you need to put ints or doubles into a list, use a standard Java array or convert them into Integer or Double objects
• In Java 5, conversion from int to Integer
and from double to Double is, in most
cases, automatic (a feature known as
autoboxing or autowrapping); the reverse
conversion (called autounboxing) is also
automatic
Trang 12ArrayList Pitfalls
// Remove all occurences
// of "like" from words:
}
Shifts all the
elements after the i-th
to the left and decrements the size
elements, a simple for loop
doesn’t work:
Trang 13“For Each” Loop
• Works with List / ArrayList:
ArrayList<String> words = new ArrayList<String> ( );
Basically the same as:
for (int i = 0; i < words.size (); i++) {
String word = words.get (i);
// process word }
Trang 14This one has a little car
Say! What a lot
of fish there are
A 12, 14, 15 ARE 16
BLACK 6 BLUE 4, 7 CAR 14 FISH 1, 2, 3, 4, 6, 7, 8, 9, 16 HAS 11, 14
LITTLE 12, 14 LOT 15
NEW 9
OF 16 OLD 8 ONE 1, 11, 14 RED 3
SAY 15 STAR 12 THERE 16 THIS 11, 14 TWO 2
WHAT 15
Trang 15Your job
extends has
Trang 16GridWorld’s Critters
• Described in Part 4 of the GridWorld
case study
• Critter is subclass of Actor
• You will create subclasses of Critter
• Study the examples provided with GridWorld:
ChameleonCritter and CrabCritter
Trang 17GridWorld’s Critters (cont’d)
public void act()
Trang 18The state of all actors in the grid remains unchanged.
void processActors(ArrayList<Actor> actors)
The state of this actor can change (except its
location) The states of actors in the actors list can
change Some of the actors from the list can be removed New actors can be added in empty grid locations All other actors in the grid remain
unchanged.
Trang 19The state of all actors in the grid remains unchanged.
The state of all actors in the grid remains unchanged.
If loc is null, this critter is removed from the grid;
otherwise this critter moves to loc This critter’s state
can change A new actor can be added in this
critter’s old location The state of all other actors in the grid remains unchanged.
Trang 21• Can a class extend ArrayList<String>?
• Can an object change after it has been added
to an ArrayList?
Trang 22• Which Critter’s methods are allowed to
change the location of this critter?