Transcript list
Lists
list: an ordered sequence of elements, each accessible
by a 0-based index
one of the most basic collections
1
List features
maintains ordering in order elements were added
(new elements are added to the end by default)
duplicates and null elements allowed
list manages its own size; user of the list does not need
to worry about overfilling it
operations: add element to end of list, insert element
at given index, clear all elements, search for element,
get element at given index, remove element at given
index, get size
2
List<T> interface
Java interface java.util.List<T> adds the following
methods to those in Collection:
public void add(int index, T element)
Inserts the specified element at the specified position in this list.
public T get(int index)
Returns the element at the specified position in this list.
public int indexOf(T element)
Returns the index in this list of the first occurrence of the specified element,
or -1 if the list does not contain it.
3
The List Interface
public interface List<E> extends Collection<E>
{
E get(int index);
E set(int index, E element);
boolean add(E element);
void add(int index, E element);
E remove(int index);
int indexOf(Object o);
int lastIndexOf(Object o);
……….
4
ArrayList
Class java.util.ArrayList implements List using an
array as the internal implementation
5
ArrayList, cont'd.
ArrayList features
think of it as an auto-resizing array, that can hold
any type of object, with many convenient methods
maintains most of the benefits of arrays, such as
fast random access
can call toString on an ArrayList to print it
remember to import java.util.*;
6
ArrayList and references
a collection such as ArrayList stores
references to its elements
if two collections refer to the same object, side
effects from one can be seen in the other
BankAccount acct = new BankAccount("Ed", 0);
ArrayList<BankAccount> list1 = new ArrayList<BankAccount>();
list1.add(acct);
ArrayList<BankAccount> list2 = new ArrayList<BankAccount>();
list2.add(acct);
acct.deposit(100.00);
System.out.println(list1);
System.out.println(list2);
7
Storing primitives in list
collections like ArrayList store Objects, not primitive
values like int, double, char, boolean
to store these primitives, we need to write special
object wrapper names when we construct the list
Integer for int
Double for double
Character for char
Boolean for boolean
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(42);
int value = list.get(0);
8
Collections class
Class java.util.Collections has many utility
methods that do useful things to collections
public
public
public
public
public
public
public
public
static
static
static
static
static
static
static
static
void copy(List<T> dest, List<T> src)
void fill(List list, Object value)
T max(Collection<T> coll)
T min(Collection<T> coll)
void reverse(List<T> list)
void shuffle(List<T> list)
void sort(List<T> list)
void swap(List<T> list, int i, int j)
9
Worksheet Example
A Grade class was defined as follows:
public class Grade
{
public Grade(double g)
{
}
public char getLetter()
{
}
public String toString()
{ return “” + String.format(“%.2f”, grade);}
}
10
Test code in client class
// instantiate an ArrayList of Grade references
ArrayList <Grade> grades = new ArrayList<Grade>();
grades
11
Test code in client class
// add 8 Grade references to list
* must instantiate (make a Grade object)
Grade g = new Grade(95.0)
95
* then add to list
grades.add(g);
grades
12
Test code in client class
// use loop to add 8 Grade objects
for(int x = 0; x < 8; x++)
{
grades.add( new Grade(Math.random()*100));
}
grades
13
Test code in client class
// To retrieve an object from list and to run the objects
methods
Grade g = grades.get(0);
95
grades
Once the object is retrieved from the list, its methods can be
invoked.
char letterGrade = g.getLetter();
A
95
14