08-ArrayList
08-ArrayList
Java
An Introduction
ERIC S. ROBERTS to Computer Science
CHAPTER 11
2
The ArrayList Class
• Although arrays are conceptually important as a data
structure, they are not used as much in Java as they are in
most other languages. The reason is that the java.util
package includes a class called ArrayList that provides the
standard array behavior along with other useful operations.
• The main differences between Java arrays and ArrayLists
stem from the fact that ArrayList is a Java class rather than
a special form in the language. As a result, all operations on
ArrayLists are indicated using method calls. For example,
the most obvious differences include:
– You create a new ArrayList by calling the ArrayList constructor.
– You get the number of elements by calling the size method rather
than by selecting a length field.
– You use the get and set methods to select individual elements.
3
The ArrayList Class
• Although arrays are conceptually important as a data
structure, they are not used as much in Java as they are in
most other languages. The reason is that the java.util
package includes a class called ArrayList that provides the
standard array behavior along with other useful operations.
• The main differences between Java arrays and ArrayLists
stem from the fact that ArrayList is a Java class rather than
a special form in the language. As a result, all operations on
ArrayLists are indicated using method calls. For example,
the most obvious differences include:
– You create a new ArrayList by calling the ArrayList constructor.
– You get the number of elements by calling the size method rather
than by selecting a length field.
– You use the get and set methods to select individual elements.
• The next slides summarize the most important methods in the
ArrayList class. The notation <T> indicates the base type.4
The ArrayList Class
int[] array1 = new int[5];
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
5
Methods in the ArrayList Class
boolean add(<T> element)
Adds a new element to the end of the ArrayList; the return value is always true.
void add(int index, <T> element)
Inserts a new element into the ArrayList before the position specified by index.
<T> remove(int index)
Removes the element at the specified position and returns that value.
boolean remove(<T> element)
Removes the first instance of element, if it appears; returns true if a match is found.
void clear()
Removes all elements from the ArrayList.
int size()
Returns the number of elements in the ArrayList.
<T> get(int index)
Returns the object at the specified index.
<T> set(int index, <T> value)
Sets the element at the specified index to the new value and returns the old value.
int indexOf(<T> value)
Returns the index of the first occurrence of the specified value, or -1 if it does not appear.
boolean contains(<T> value)
Returns true if the ArrayList contains the specified value.
boolean isEmpty()
6
Returns true if the ArrayList contains no elements.
The ArrayList Class
int[] array1 = new int[5];
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
7
The ArrayList Class
int[] array1 = new int[5];
0 0 0 0 0
8
The ArrayList Class
int[] array1 = new int[5];
0 1 0 0 0
9
The ArrayList Class
int[] array1 = new int[5];
0 1 2 0 0
10
The ArrayList Class
int[] array1 = new int[5];
0 1 2 3 0
11
The ArrayList Class
int[] array1 = new int[5];
0 1 2 3 4
12
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
13
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
14
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
15
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
0 1
16
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
0 1 2
17
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
0 1 2 3
18
The ArrayList Class
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
0 1 2 3 4
19
The ArrayList Class
int[] array1 = new int[5];
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
for(int i = 0; i < 5; i++) {
array1[i] = i;
println(array1.length);
}
println(“----------”)
for(int i = 0; i < 5; i++) {
// add a new element to the end of the list
arrayList1.add(i);
println(arrayList1.size());
}
20
The ArrayList Class
int[] array1 = new int[5];
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
for(int i = 0; i < 5; i++) {
array1[i] = i;
println(array1.length);
}
println(“----------”)
for(int i = 0; i < 5; i++) {
// add a new element to the end of the list
arrayList1.add(i); 5
println(arrayList1.size()); 5
} 5
5
5
----------
add method extends the size by 1
2
one by adding a new element to 3
the end of the list. 4
5
21
The ArrayList Class
array[2] = 42;
println(array[2]);
arrayList.set(2, 42);
println(arrayList.get(2));
22
The ArrayList Class
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(42);
arrayList.add(43);
arrayList.add(44);
arrayList.set(2, 45);
println(arrayList.get(2)); ?
23
The ArrayList Class
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(42);
arrayList.add(43);
arrayList.add(44);
arrayList.set(2, 45);
println(arrayList.get(2)); // prints
? 45
24
The ArrayList Class
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Double> doubleList = new ArrayList<Double>();
ArrayList<String> stringList = new ArrayList<String>();
intList.add(42);
doubleList.add(42.0);
stringList.add(“42”);
25
Generic Types in Java 5.0
• The <T> notation used on the preceding slide is a new feature
of Java that was introduced with version 5.0 of the language.
In the method descriptions, the <T> notation is a placeholder
for the element type used in the array. Class definitions that
include a type parameter are called generic types.
• When you declare or create an ArrayList, it is a good idea
to specify the element type in angle brackets. For example, to
declare and initialize an ArrayList called names that
contains elements of type String, you would write
ArrayList<String> names = new ArrayList<String>();
31
public static void printStringArrayList(ArrayList<String> list) {
for(int i = 0; i < list.size(); i++){
System.out.print(" " + list.get(i));
}
System.out.println();
}
32
import java.util.ArrayList;
33