ArrayList class
Download
Report
Transcript ArrayList class
Arrays and Collections
Intro to the Java Collections
Framework
Portions adapted with permission from the textbook
author.
SE-1020
Dr. Mark L. Hornick
1
Modern programming languages
support the concept of collections
That is, mechanisms to sort and manipulate
many instances of the same type of data
In Java, an array is an indexed collection of
data values of the same type
Arrays of both primitives or objects are possible
Winter 2004
Integer arrays
String arrays
WinPlotter arrays
CS-1010
Dr. Mark L. Hornick
2
Using constants to declare the array sizes
does not always lead to efficient use of
space
rainfall = new double [12];
Declaration of arrays with constants is called fixed-size
array declaration.
Fixed-size array declaration may pose two problems;
either:
Winter 2004
Not enough capacity for the task at hand.
Wasted space (too much capacity)
CS-1010
Dr. Mark L. Hornick
3
Java is not limited to fixedsize array declaration
int size;
int[] number;
size= Integer.parseInt(
JOptionPane.showInputDialog(null,
“Enter the size of an array:”));
number = new int[size]; // size determined from user input
Winter 2004
CS-1010
Dr. Mark L. Hornick
4
Any valid integer arithmetic
expression is allowed for
specifying the size of an array
size = Integer.parseInt(
JOptionPane.showInputDialog(null,””));
number = new int[size*size + 2* size + 5];
Winter 2004
CS-1010
Dr. Mark L. Hornick
5
Object Deletion
int delIdx = 1;
A
person
person
0
A
Delete Person B by
setting the reference in
position 1 to null.
person[delIdx] = null;
1
B
2
C
3
0
D
A
Before A is executed
Portions adapted with permission from the textbook
author.
1
2
C
3
D
After A is executed
SE-1020
Dr. Mark L. Hornick
6
Java Collections Framework
The java.util standard package contains other types
of classes for maintaining a collection of objects.
We can add to, remove from, and retrieve objects in a
collection.
A collection does not have a set limit to the number of
objects we can add to it.
JCF includes classes that maintain collections of
objects as sets, lists, or maps.
CS2851 is an entire course based on JCF
Portions adapted with permission from the textbook
author.
SE-1020
Dr. Mark L. Hornick
7
Advantages to using JCF
collection classes
Internal structure is hidden from view
Code is optimized for performance
Code is tested for quality
More flexible
Portions adapted with permission from the textbook
author.
SE-1020
Dr. Mark L. Hornick
8
ArrayList is a collection class that implements
the following methods (and others)
boolean add( Object o )
Adds an object o to the list
void clear( )
Clears this list, i.e., make the list empty
Object get( int idx
)
Returns the element at position idx
boolean remove( int idx
)
Removes the element at position idx
int size()
Returns the number of elements in the list
Portions adapted with permission from the textbook
author.
SE-1020
Dr. Mark L. Hornick
9
Generic ArrayList<E>
ArrayList<Double> salaryList
= new ArrayList<Double>();
E specifies the specific type of data that the ArrayList
can manage
This creates an instance, salaryList, of the ArrayList
collection class. The elements in salaryList must be
(references to) Doubles.
You must substitute a class name for E; you cannot create an
ArrayList of primitives
Double is the wrapper class for the primitive type double.
Generics allow you to create a class template that can use
different types of objects when
instantiated
CS-2851
Dr. Mark L. Hornick
10
Type conversion is done automatically
through boxing and unboxing
Example: What if you want to insert a double
value into an ArrayList<Double> at index 30?
salaryList.add(30, 40000.00);
This is called boxing: The automatic conversion of a
primitive (double) value to the appropriate (Double)
wrapper object
To retrieve the value, no explicit cast is needed:
double pay = /*(double)*/ salaryList.get(30);
This is because although the get() method returns a
Double, a Double can be converted to a double primitive
type. This is called unboxing.
CS-2851
Dr. Mark L. Hornick
11
ArrayList<E> usage features
What if salaryList needs to be expanded?
Done automatically!
What if you need to know the number
of elements currently in salaryList?
salaryList.size()
What if you want to insert the element
at index 30?
salaryList.add (30, 40000.00);
// note: old element 30 becomes element 31, 31->32, etc
To retrieve the value, no cast is needed:
double sum = 0;
sum = sum + salaryList.get(30);
…provided that you use the generic version of ArrayList
Portions adapted with permission from the textbook
author.
SE-1020
Dr. Mark L. Hornick
12
Built-in automatic sorting
Suppose you randomly add elements to an
ArrayList
You can use the static sort() method of the
Collections class to sort them
Collections.sort(list); // list is an
ArrayList
Portions adapted with permission from the textbook
author.
SE-1020
Dr. Mark L. Hornick
13