Lists - Java
Lists - Java
Lists - Java
• A List ADT
• Types of Lists
• Lists in Java Collections API
• Using ordered lists – Tournament Maker
• Using indexed lists – Josephus Problem
• Implementing lists with arrays and links
• Analysis of list implementations
• Reading L&C 6.1-6.6
1
A List ADT
• A list a collection of items in which the
items have a position
• It is an inherently familiar concept
• We keep “to-do” lists, shop with a grocery
list, and invite a list of friends to a party
• Types of Lists
– Ordered Lists
– Unordered Lists
– Indexed Lists
2
Ordered Lists
• An ordered list is kept in order based on
characteristics of the elements in the list,
e.g. alphabetical order for names
• The class for objects kept in an ordered list
must implement the Comparable interface
• When an element (object) is added to an
ordered list, its position relative to the other
elements stored in the list is determined by
the compareTo method for their class
3
Unordered Lists
• An unordered list is not based on any
characteristics of the elements themselves
• The class for the elements does not need to
implement the Comparable interface for them
to be stored in an unordered list
• They are stored in an order that is externally
controlled by how and when the elements
are added to the list
4
Indexed Lists
• An indexed list is an unordered list that also
allows elements to be added or removed
based on a index value
• Again the order is externally controlled by
how and when the elements are added, but
maybe by the index value used when adding
• You should be familiar with the ArrayList
class which is a class for an Indexed list
5
Ordered versus Indexed Lists
• Is it meaningful for a list to be both ordered
and indexed?
• No - The two concepts are not compatible
• In an ordered list, elements should be kept
in the order determined by the compareTo
method of the class of the elements
• In an indexed list, elements should be kept
in the order indicated by the indices used
when adding the elements
6
Text’s List Interface Hierarchy
<<interface>>
OrderedListADT<T>
+ add(element: T) : void
<<interface>>
ListADT<T> <<interface>>
UnorderedListADT<T>
+ removeFirst() : T + addToFront(element: T) : void
+ removeLast() : T + addToRear(element: T) : void
+ remove(element :T) : T + addAfter(element: T, target : T): void
+ first() : T
+ last() : T <<interface>>
+ contains(target : T): boolean IndexedListADT<T>
+ isEmpty() : boolean
+ size() : int + add(index: int, element: T) : void
+ iterator() : Iterator<T> + add(element: T) : void
+ toString() : String + set(index: int, element: T) : void
+ get(index: int) : T
+ indexOf(element :T) : int
+ remove(index: int) : T 7
Using Ordered Lists
• In a tournament, teams play pairwise with
each other in a series of games until the
last two teams play in championship game
• To “seed” the tournament in the first round,
the best team plays the worst team, the
next best team plays the next worst team,
and all teams are paired this way
• This is “fairest” because it is impossible for
the best team to play the second best team
in the first round – eliminating one of them
8
Using Ordered Lists
• Tournament Maker (for a number of teams
that is a power of 2)
• Algorithm is based on adding the teams to
an ordered list where the team compareTo
method uses the number of wins and that
determines each team’s order in the list
• Algorithm removes first and last team and
pairs them for a game until all teams have
been paired for a game (i.e, list is empty)
9
Using Ordered Lists
• For subsequent rounds, the games can be
paired based on their game number
• Since the game numbers were determined
based on the number of wins of the teams
this algorithm will build out the subsequent
rounds of play correctly to make it most
likely that the best and second best teams
make it to the championship game
10
UML for Tournament Maker
OrderedListADT
implements
Team
ArrayOrderedList
extends
ArrayList
TournamentMaker Tournament
implements
{main}
ListADT
12
Using Indexed Lists
• Josephus Solution based on an ArrayList
int numPeople = 7, gap = 3;
int counter = gap - 1;
ArrayList<Integer> list=new
ArrayList<Integer>(); // init to 1, 2, ...
...
while (!list.isEmpty())
{
System.out.println(list.remove(counter));
numPeople = numPeople - 1;
if (numPeople > 0)
counter = (counter + gap - 1) % numPeople;
}
13
Josephus Problem
• A recursive solution to the Josephus problem that
provides the survivor’s number for gap = 2 only
public static int J(int n) // n is number of soldiers
{
if (n == 1)
return 1; // base case
else if (n % 2 == 0)
return 2 * J(n/2) - 1; // n is even
else
return 2 * J(n/2) + 1; // n is odd
}
nodePtr
next
Target next
for
previous Remove previous 16
Lists in Java Collections API
• These are the three primary list classes:
java.util.ArrayList – a list based on an
underlying array
java.util.LinkedList – a list based on an
underlying linked structure
java.util.Vector – a list that is more often
used for backward compatibility today and is
no longer “in vogue” for new applications
(although some of its subclasses such as
java.util.Stack are still of interest)
17
Review: Collection Interface Hierarchy
<<interface>>
Iterable
<<interface>>
Collection
18
Java Collection API List Hierarchy
<<interface>> <<abstract>> <<interface>>
List AbstractCollection Collection
<<abstract>> <<abstract>>
AbstractList AbstractSequentialList
ArrayList LinkedList
19
Analysis of List Implementations
• Much of the time analysis is similar to the
analysis of methods for previous structures
• Some methods that are O(1) for the array
implementation are O(n) for linked version
– Set or get element at the specified index
• Some methods that are O(1) for the linked
implementation are O(n) for array version
– Add to front
20
Analysis: ArrayList vs LinkedList
• The ArrayList is more efficient
– If items are often added to the end of the list
– If items are often retrieved by index value
• The ArrayList is less efficient:
– If items are often added in the middle (all of the
following items must be moved out of the way)
• The LinkedList is more efficient:
– If items are often added to the start of the list
• The LinkedList is less efficient:
– If items are often retrieved by index value 21