Professional Documents
Culture Documents
The List Interface and Arraylist Class Chapter Xiii Topics
The List Interface and Arraylist Class Chapter Xiii Topics
The List Interface and Arraylist Class Chapter Xiii Topics
13.1 Introduction
13.2 What is an Interface?
13.3 The Collection Hierarchy
13.4 Java Collection Classes
13.5 ArrayList Methods
13.6 ArrayList and Primitive Data
Types
13.7 ArrayList and Generics
13.8 ArrayList and the Enhanced
For Loop
13.9 Summary
Java Arrays
Power on/off
Volume lower/higher
Channels up/down
Switch between
TV/VCR
Play/pause/fast-
forward/rewind VCR
Program VCR
Java Interface
Collection
Linear Collections
Linear Collections
Lists
Lists
Unordered Collections
Unordered Collections
Bags
Sets
Figure 13.1
Collection
interface
List Set
interface interface
import java.util.*;
System.out.println("\nJAVA1301.JAVA\n");
Collection collect = new Collection();
System.out.println("Collection contains: "
+ collect);
}
}
Java1301.java:15:
java.util.Collection is
abstract; cannot be
instantiated
Collection
collect = new Collection();
^
1 error
Figure 13.2
// Java1302.java
// This program generates the same
Chapter XIII The List Interface and ArrayList Class 641
compile error message as the previous
program.
// <List> and <Set> are interfaces and
cannot be instantiated.
// <List> and <Set> are sometimes called
"subinterfaces."
import java.util.*;
System.out.println("\nJAVA1302.JAVA\n");
List list = new List();
Set set = new Set();
System.out.println("list contains: " + list);
Java1302.java:15:
java.util.List is abstract;
cannot be instantiated
List list =
new List();
^
Java1302.java:16:
java.util.Set is abstract;
cannot be instantiated
Set set = new
Set();
Figure 13.4
// Java1303.java
// This program demonstrates the four
<Collection> classes.
import java.util.*;
System.out.println("\nJAVA1303.JAVA\n");
ArrayList object1 = new ArrayList();
LinkedList object2 = new LinkedList();
HashSet object3 = new HashSet();
TreeSet object4 = new TreeSet();
Java1303.java Output
JAVA1303.JAVA
object1 contains []
object2 contains []
object3 contains []
object4 contains []
Figure 13.5
// Java1304.java
// This program is almost identical to the
previous program.
// The same four objects are instantiated
with each of the four
// <Collection> classes. This time note that
the class on the
// left side of the object actually is the
Chapter XIII The List Interface and ArrayList Class 647
<Collection> interface.
// This is not only allowed, but frequently
very desirable for
// proper class interaction.
import java.util.*;
System.out.println("\nJAVA1304.JAVA\n");
Collection object1 = new ArrayList();
Collection object2 = new LinkedList();
Collection object3 = new HashSet();
Collection object4 = new TreeSet();
Java1304.java Output
JAVA1304.JAVA
object1 contains []
object2 contains []
object3 contains []
object4 contains []
Figure 13.6
// Java1305.java
// This program demonstrates the <add>
method of the <Collection> interface.
// The <add> method is implemented in
each one of the four <Collection>
// classes. This program may give the
impression that the <add> method
// is identical in each one of the four
classes.
System.out.println("\nJAVA1305.JAVA\n");
Collection object1 = new ArrayList();
Collection object2 = new LinkedList();
Collection object3 = new HashSet();
Collection object4 = new TreeSet();
Java1305.java Output
JAVA1305.JAVA
Figure 13.7
// Java1306.java
// This program demonstrates that <List>
classes can store duplicate
// elements, but <set> classes do not store
duplicate elements.
import java.util.*;
Chapter XIII The List Interface and ArrayList Class 653
public class Java1306
{
public static void main(String args[])
{
System.out.println("\nJAVA1306.JAVA\n");
Collection object1 = new ArrayList();
Collection object2 = new LinkedList();
Collection object3 = new HashSet();
Collection object4 = new TreeSet();
Java1306.java Output
JAVA1306.JAVA
Figure 13.8
// Java1307.java
// This program demonstrates the <add>
method of the <ArrayList> class.
// Note that each name is added to the
end of the list.
import java.util.ArrayList;
System.out.println("Java1307.java\n");
Java1307.java
names.add("Tom");
System.out.println(names);
Figure 13.9
// Java1308.java
// This program uses the <size> method
to determine the number of elements
// in an <ArrayList> object.
// Note that the value return by the
<size> method changes when more
names
// are added to the <ArrayList> object.
import java.util.ArrayList;
System.out.println("Java1308.java\n");
names.add("Maria");
names.add("Heidi");
System.out.println("names contains "
+ names);
System.out.println("There are " +
names.size() + " elements in the names
object.");
System.out.println();
}
Java1308.java Output
Java1308.java
Figure 13.10
// Java1309.java
Chapter XIII The List Interface and ArrayList Class 665
// This program shows how to access
specified elements in an <ArrayList>
object
// with the <get> method.
import java.util.ArrayList;
System.out.println("Java1309.java\n");
System.out.println();
System.out.println();
System.out.println();
}
}
Java1309.java
Isolde
John
Greg
Maria
Chapter XIII The List Interface and ArrayList Class 667
Heidi
Heidi
Maria
Greg
John
Isolde
System.out.println(names.get(3));
Figure 13.11
// Java1310.java
// This program demonstrates the <set>
method of the <ArrayList> class, which
Chapter XIII The List Interface and ArrayList Class 669
// replaces existing elements with a new
object.
import java.util.ArrayList;
System.out.println("Java1310.java\n");
Java1310.java Output
Java1310.java
names.set(4,"Tom");
import java.util.ArrayList;
System.out.println("Java1311.java\n");
ArrayList names = new ArrayList();
names.add("Isolde");
names.add("John");
names.add("Greg");
names.add("Maria");
names.add("Heidi");
System.out.println("names contains
Chapter XIII The List Interface and ArrayList Class 673
" + names);
System.out.println();
names.remove(2);
System.out.println("names contains "
+ names);
System.out.println();
names.remove(3);
System.out.println("names contains "
+ names);
System.out.println();
}
}
Java1311.java Output
Java1311.java
names.remove(3);
Figure 13.13
// Java1312.java
// This program demonstrates how to
use the <add> method of the
<ArrayList> class to
import java.util.ArrayList;
System.out.println("Java1312.java\n");
names.add(3,"Anthony");
Java1312.java
names.add(3,"Kathy");
Figure 13.14
// Java1313.java
// This program demonstrates that <int>
values stored into an <ArrayList> object
// must first be converted to <Integer>
objects.
Chapter XIII The List Interface and ArrayList Class 683
// <ArrayList> can only store objects
members, not primitive data types.
// Initially, this program compiles, and
executes. If you remove the comments
// from the program an attempt is made
to add the values of the <numbers>
// object, which is not possible.
import java.util.ArrayList;
import java.util.Random;
System.out.println("Java1313.java\n");
Random rand = new
Random(12345);
ArrayList numbers = new ArrayList();
// int sum = 0;
// for (int k = 0; k < numbers.size(); k++)
// {
// sum += numbers.get(k);
// }
// System.out.println("Sum: " + sum);
System.out.println();
}
}
1313.java Output
Java1313.java
Figure 13.15
// Java1314.java
// This program has no output, which
does not matter, because it does not
compile.
// You will see two "imcompatible types"
syntax errors. This may seem strange
// because the <ArrayList> object stores
<Person> objects.
import java.util.ArrayList;
class Person
{
private String name;
private int age;
C:\Users\leonschram\Desktop\AP
CSChapters2009\ChapterProgsAPC
S\Progs13\Backup\Java1314.java
:21: incompatible types
found : java.lang.Object
required: Person
Person student1 =
people.get(0);
^
C:\Users\leonschram\Desktop\AP
CSChapters2009\ChapterProgsAPC
S\Progs13\Backup\Java1314.java
:22: incompatible types
found : java.lang.Object
required: Person
Person student2 =
people.get(1);
^
Note:
C:\Users\leonschram\Desktop\AP
CSChapters2009\ChapterProgsAPC
Figure 13.16
// Java1315.java
// This program compiles and there is
import java.util.ArrayList;
System.out.println("Java1315.java\n");
class Person
{
private String name;
private int age;
import java.util.ArrayList;
System.out.println("Java1315.java\n");
class Person
{
private String name;
private int age;
Java1316.java
Figure 13.18
// Java1317.java
// This program shows another benefit of
using generics.
Chapter XIII The List Interface and ArrayList Class 697
// There are two <ArrayList> objects and
both are constructed
// to store <Integer> values. After three
values are entered
// in the <numbers1> object, those
values are then assigned
// to <numbers2>, which works without
problems.
import java.util.ArrayList;
System.out.println("Java1317.java\n");
System.out.println();
}
}
Java1317.java Output
Java1317.java
Figure 13.19
// Java1318.java
// Generics make sure that an array is in
fact an array. An array
// is supposed to be a data structure with
elements of the same type.
// This program example - which does
not use generics - allows the
// list array to store three different data
types.
System.out.println("Java1318.java\n");
Java1318.java Output
Figure 13.20
// Java1319.java
// Once generics are used, Java
becomes very picky. If you want to
create
// an <ArrayList> object to store
<Double> values, such as is shown
below,
// then only <Double> values must be
added.
import java.util.ArrayList;
System.out.println("Java1318.java\n");
C:\Users\leonschram\Desktop\Pr
ogs132009\Progs13\Backup\Java1
319.java:21: cannot find
symbol
symbol : method
add(java.lang.Integer)
location: class
java.util.ArrayList<java.lang.
Double>
list.add(new
Integer(200));
^
C:\Users\leonschram\Desktop\Pr
ogs132009\Progs13\Backup\Java1
319.java:22: cannot find
symbol
symbol : method
add(java.lang.String)
location: class
java.util.ArrayList<java.lang.
Double>
import java.util.ArrayList;
System.out.println("Java1319.java\n");
System.out.println(names);
System.out.println();
System.out.println(names.get(index));
System.out.println();
Java1320.java
Isolde
John
Greg
Maria
Heidi
Isolde
John
Greg
Maria
Heidi
names.add("Tom");
System.out.println(names.get(3));
names.set(4,"Tom");
names.remove(3);
names.add(3,"Kathy");