Lists - I: The List ADT Textbook Sections 3.1 - 3.4
Lists - I: The List ADT Textbook Sections 3.1 - 3.4
List ADT
8/3/2007
8/3/2007
printList is O(n)
findkth,get and set are constant time
Insert and delete?
8/3/2007
Deletion
8/3/2007
8/3/2007
8/3/2007
8/3/2007
10
8/3/2007
11
The ListIterator<E>
Interface
8/3/2007
12
Concrete Implementations of
the List ADT in the Java
Collections
API
Two concrete implementations
of the List API
in the Java Collections API with which you
are already familiar are:
java.util.ArrayList
java.util.LinkedList
8/3/2007
13
List Operations on an
ArrayList<E>
8/3/2007
14
List Operations on an
ArrayList<E> (cont.)
8/3/2007
15
List Operations on a
LinkedList<E>
8/3/2007
16
List Operations on a
LinkedList<E>
void addFirst(E o)
void addLast(E o)
deletion from the beginning of the list using
E removeFirst()
deletion from the end of the list using
E removeLast()
Accessing first element of the list using
E getFirst()
E getLast()
8/3/2007
17
List Operations on a
LinkedList<E>
8/3/2007
18
8/3/2007
19
8/3/2007
20
public static
int sum(List<Integer> list, int N)
{
int total = 0;
for(int i = 0; i < N ; i++)
total += list.get(i);
return total;
}
How can we change this code so the running time for both is
the same?
8/3/2007
21
8/3/2007
22
8/3/2007
23
8/3/2007
24
6. add Methods
public boolean add( AnyType x ){
add( size( ), x );
return true;
}
public void add( int idx, AnyType x ){
if( theItems.length == size( ) )
ensureCapacity( size( ) * 2 + 1 );
for( int i = theSize; i > idx; i-- )
theItems[ i ] = theItems[ i - 1 ];
theItems[ idx ] = x;
theSize++;
}
8/3/2007
27
8/3/2007
28
8. Iterator class
// private inner class for iterator
private class ArrayListIterator implements
java.util.Iterator<AnyType>
{
private int current = 0;
Implicit reference to
outer class method
Explicit reference to
outer class method
}
8/3/2007
29
8/3/2007
30
Sorted Lists
8/3/2007
31