Professional Documents
Culture Documents
ArrayList and Iterator
ArrayList and Iterator
ArrayList and Iterator
Programming
ArrayLists and arrays
A ArrayList is like an array of Objects
Differences between arrays and ArrayLists:
Arrays have special convenience syntax; ArrayLists don’t
An array is a fixed size, but a ArrayList expands as you add things to it
This means you don’t need to know the size beforehand
Arrays can hold primitives or objects, but ArrayLists can only hold objects
However, autoboxing can make it appear that an ArrayList can hold primitives
2
Generics
In Java versions 1.4 and earlier, an ArrayList just held Objects, which
could be of any (non-primitive) type
For compatibility reasons you can still do this, but you will get a lot of
warning messages
In Java 5 you should specify the class of objects that the ArrayList will hold
This is done with the new generics syntax
3
Creating a ArrayList the old way
The syntax for creating ArrayLists has changed between Java 1.4 and Java
5
For compatibility reasons, the old way still works, but will give you warning
messages
Here are the (old) constructors:
import java.util.ArrayList;
ArrayList vec1 = new ArrayList();
Constructs an ArrayList with an initial capacity of 10
4
Using an ArrayList the old way
5
Creating a ArrayList the new way
Specify, in angle brackets after the name, the type of object that the class
will hold
Examples:
ArrayList<String> vec1 = new ArrayList<String>();
ArrayList<String> vec2 = new ArrayList<String>(10);
To get the old behavior, but without the warning messages, use the <?>
wildcard
Example: ArrayList<?> vec1 = new ArrayList<?>();
6
Adding elements to a ArrayList
boolean add(Object obj)
Appends the object obj to the end of this ArrayList
With generics, the obj must be of the correct type, or you
get a compile-time (syntax) error
Always returns true
This is for consistency with other, similar classes
7
Removing elements
8
Accessing with and without generics
9
Searching a ArrayList
10
Getting information
boolean isEmpty()
Returns true if this ArrayList has no elements
int size()
Returns the number of elements currently in this ArrayList
Object[ ] toArray()
Returns an array containing all the elements of this ArrayList in the correct order
11
More about equals
12
13 Iterators
E next()
//returns the next element in this iteration
//pre: hastNext()
void remove()
/*Removes from the underlying collection the last element returned by the
iterator.
pre: This method can be called only once per call to next. After calling, must
call next again before calling remove again.
*/
16 Clicker Question 1
Which of the following produces a syntax error?
ArrayList<String> list
= new ArrayList<String>();
Iterator<String> it1 = new Iterator(); // I
Iterator<String> it2 = new Iterator(list); // II
Iterator<String> it3 = list.iterator(); // III
A. I
B. II
C. III
D. I and II
E. II and III
17 Iterator
rails
Imagine a fence made up of fence posts and rail sections
fenceposts
18 Fence Analogy
The iterator lives on the fence posts
The data in the collection are the rails
Iterator created at the far left post
As long as a rail exists to the right of the Iterator, hasNext() is true
iterator object
19 Fence Analogy
ArrayList<String> names =
new ArrayList<String>();
names.add(“Jan”);
names.add(“Levi”);
names.add(“Tom”);
names.add(“Jose”);
Iterator<String> it = names.iterator();
int i = 0;
while( it.hasNext() ) {
i++;
System.out.println( it.next() );
}
// when i == 2, prints out Levi
while( it.hasNext() ) {
i++;
System.out.println( it.next() );
}
// when i == 3, prints out Tom
while( it.hasNext() ) {
i++;
System.out.println( it.next() );
}
// when i == 4, prints out Jose
while( it.hasNext() ) {
i++;
System.out.println( it.next() );
}
// call to hasNext returns false
// while loop stops