chapter 2 lists and the collections framework. chapter objectives the list interface writing an...
TRANSCRIPT
![Page 1: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/1.jpg)
CHAPTER 2Lists and theCollections Framework
![Page 2: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/2.jpg)
Chapter Objectives
The List interface Writing an array-based implementation of List Linked list data structures:
Singly-linked Doubly-linked Circular
Big-O notation and algorithm efficiency Implementing the List interface as a linked list The Iterator interface Implementing Iterator for a linked list Testing strategies The Java Collections framework (hierarchy)
2
![Page 3: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/3.jpg)
Introduction
A list a collection of elements, each with a position or
index Iterators
facilitate sequential access to a list Classes ArrayList, Vector, and LinkedList subclasses of abstract class AbstractList implement the List interface
3
![Page 4: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/4.jpg)
Section 2.1
The List Interface and ArrayList Class
4
![Page 5: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/5.jpg)
Array, Java Array object
Array - an indexed structure Random access Sequential access
Java Array object Fixed length Hard to change its length
add an element remove an element
5
![Page 6: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/6.jpg)
List Interface
part of JAVA API java.util List operations:
Obtain an element at a specified position Replace an element at a specified position Find a specified target value Add an element at either end Remove an element from either end Insert or remove an element at any position Traverse the list structure without managing
a subscript
6
![Page 7: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/7.jpg)
java.util.List Interface and its Implementers
7
![Page 8: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/8.jpg)
ArrayList Class
8
Implement List interface It cannot store primitive types Classes must store values as objects How to store primitive types?
![Page 9: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/9.jpg)
ArrayList Class, … more
An improvement over an array object adding new elements to the end of a list access elements quickly in any order
9
![Page 10: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/10.jpg)
Example
List<String> myList = new ArrayList<String>();
Initial value - empty initial capacity -10 elements add elements to “myList”,
myList.add("Bashful");
myList.add("Awful");
myList.add("Jumpy");
myList.add("Happy");
10
![Page 11: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/11.jpg)
Add in between
Adding an element with subscript 2:
myList.add(2, "Doc");
Notice that the subscripts of "Jumpy" and "Happy" have changed from [2],[3] to [3],[4]
11
![Page 12: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/12.jpg)
Add at the end
myList.add("Dopey");
12
![Page 13: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/13.jpg)
Remove an element
Removing an element:
myList.remove(1);
The strings referenced by [2] to [5] have changed to [1] to [4]
13
![Page 14: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/14.jpg)
Replace an element
myList.set(2, "Sneezy");
14
![Page 15: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/15.jpg)
Access an element
myList[1]?
OR
String dwarf = myList.get(1);
15
![Page 16: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/16.jpg)
Get index of an element
myList.indexOf("Sneezy"); Returns?
myList.indexOf("Jumpy"); Returns?
16
![Page 17: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/17.jpg)
Generic Collections
The statementList<String> myList = new
ArrayList<String>(); Generic collections or Generics “String” -- type parameter It sets the data type of all objects stored
in a collection
17
![Page 18: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/18.jpg)
Generic Collections: Primitive type example
General declaration CollectionClassName<E> variable =
new CollectionClassName<E>();
“E” is a type parameter Adding a noncompatible type How about primitive types ?
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(new Integer(3)); // ok
myList.add(3); // also ok! 3 is automatically wrapped
in an Integer object
myList.add(new String("Hello")); // error!
18
![Page 19: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/19.jpg)
Why Use Generic Collections? Flexible
List<T> List<int>, or List<string>,… Enhance “type-checking”
19
List v = new ArrayList(); v.add("test"); Integer i = (Integer)v.get(0); // Run time error
List<String> v = new ArrayList<String>(); v.add("test"); Integer i = v.get(0); // (type error) Compile time error
![Page 20: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/20.jpg)
ArrayList: Methods20
![Page 21: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/21.jpg)
Section 2.2
Applications of ArrayList21
![Page 22: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/22.jpg)
Application of ArrayList
ArrayList<Integer> someInts = new ArrayList<Integer>();
int[] nums = {5, 7, 2, 15};
for (int i = 0; i < nums.length; i++) {
someInts.add(nums[i]);
}
// Display the sum
int sum = 0;
for (int i = 0; i < someInts.size(); i++) {
sum += someInts.get(i);
}
System.out.println("sum is " + sum);
22
![Page 23: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/23.jpg)
The Example
ArrayList<Integer> someInts = new ArrayList<Integer>();
int[] nums = {5, 7, 2, 15};
for (int i = 0; i < nums.length; i++) {
someInts.add(nums[i]);
}
// Display the sum
int sum = 0;
for (int i = 0; i < someInts.size(); i++) {
sum += someInts.get(i);
}
System.out.println("sum is " + sum);
nums[i] is an int; it is automatically wrapped in
an Integer object
23
![Page 24: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/24.jpg)
Another Application:Phone Directory
public class DirectoryEntry {
String name;
String number;
} Create a class for objects stored in
the directory
24
![Page 25: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/25.jpg)
Phone Directory Example
public class DirectoryEntry {
String name;
String number;
}
private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>();
Create the directory
25
![Page 26: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/26.jpg)
Add an Entry
public class DirectoryEntry {
String name;
String number;
}
private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>();
theDirectory.add(new DirectoryEntry("Jane Smith",
"555-1212"));
Add a DirectoryEntry
object
26
![Page 27: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/27.jpg)
Search Phone Directory by “Name”
public class DirectoryEntry {
String name;
String number;
}
private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>();
theDirectory.add(new DirectoryEntry("Jane Smith",
"555-1212"));
int index = theDirectory.indexOf(new DirectoryEntry(aName,
""));
Method indexOf searches theDirectory by applying the equals
method for class DirectoryEntry. Assume DirectoryEntry's equals method compares name fields.
27
![Page 28: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/28.jpg)
Access Phone Directory by “index”
public class DirectoryEntry {
String name;
String number;
}
private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>();
theDirectory.add(new DirectoryEntry("Jane Smith", "555-1212"));
int index = theDirectory.indexOf(new DirectoryEntry(aName, ""));
if (index != -1)
dE = theDirectory.get(index);
else
dE = null;
28
![Page 29: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/29.jpg)
Section 2.3
Implementation of an ArrayList Class
29
![Page 30: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/30.jpg)
Fields: Capacity and Size
KWArrayList: Capacity: Physical size Size: Number of data items stroed
30
![Page 31: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/31.jpg)
KWArrayList Fields
import java.util.*;
/** This class implements some of the methods of the Java ArrayList class
*/
public class KWArrayList<E> {
// Data fields
/** The default initial capacity */
private static final int INITIAL_CAPACITY = 10;
/** The underlying data array */
private E[] theData;
/** The current size */
private int size = 0;
/** The current capacity */
private int capacity = 0;
}
31
![Page 32: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/32.jpg)
KWArrayList Constructor
public KWArrayList () {
capacity = INITIAL_CAPACITY;
theData = (E[]) new Object[capacity];
}
This statement allocates storage for an array of type
Object(superclass for all types) and then casts the array object to
type E[]
Although this may cause a compiler warning, it's ok
32
![Page 33: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/33.jpg)
Implementing ArrayList.add(E)
Two add methods append at the end of the list insert an item at a specified position
33
![Page 34: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/34.jpg)
Add at the end
Check the size insert the new item at the “size” position size=size++
34
![Page 35: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/35.jpg)
Implementing Add in between ArrayList.add(int index,E anEntry)
Check size size = size + 1 Make room first Insert at “index” position
35
![Page 36: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/36.jpg)
Implementing ArrayList.add(index,E)
public void add (int index, E anEntry) {
// check bounds if (index < 0 || index > size) { throw new ArrayIndexOutOfBoundsException(index); }
// Make sure there is room if (size >= capacity) { reallocate(); }
// shift data for (int i = size; i > index; i--) { theData[i] = theData[i-1]; }
// insert item theData[index] = anEntry; size++;}
36
![Page 37: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/37.jpg)
set and get Methods
public E get (int index) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException(index);
}
return theData[index];
}
public E set (int index, E newValue) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } E oldValue = theData[index]; theData[index] = newValue; return oldValue;}
37
![Page 38: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/38.jpg)
remove Method: idea
Check “index” Shift left size = size - 1
38
![Page 39: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/39.jpg)
remove Method: the code
public E remove (int index) {
if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); }
E returnValue = theData[index];
for (int i = index + 1; i < size; i++) { theData[i-1] = theData[i]; }
size--; return returnValue;}
39
![Page 40: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/40.jpg)
reallocate Method
Create a new array with double size Copy the contents of the new array
private void reallocate () { capacity *= 2; theData = Arrays.copyOf(theData, capacity);}
40
![Page 41: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/41.jpg)
reallocate Method: doubling
private void reallocate () { capacity *= 2; theData = Arrays.copyOf(theData, capacity);}
The reason for doubling is to spread out the cost of copying; we discuss this further in the next section
41
![Page 42: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/42.jpg)
KWArrayList as a Collection of Objects: old version
Earlier versions of Java did not support generics; all collections contained only Object elements
To implement KWArrayList this way, remove the parameter type <E> from the
class heading, replace each reference to data type E by Object
The underlying data array becomes private Object[] theData;
42
![Page 43: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/43.jpg)
Vector/ArrayList Class
Vector and ArrayList are similar ArrayList is more efficient Vector class is synchronized
Support multiple threads access a Vector object without confliction
43
![Page 44: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/44.jpg)
Section 2.4
Algorithm Efficiency and Big-O
44
![Page 45: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/45.jpg)
Algorithm Efficiency/Big-O the performance of an algorithm
Hard to measure precisely Big-O notation is a choice
Big-O notation Function of the size of a problem Used to compare the algorithms’ efficiency
45
![Page 46: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/46.jpg)
Linear Growth Rate
The processing time is proportional to the problem size
the algorithm grows at a linear rate
public static int search(int[] x, int target) {
for(int i=0; i < x.length; i++) {
if (x[i]==target)
return i;
}
return -1; // target not found
}
46
![Page 47: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/47.jpg)
Linear Growth Rate
public static int search(int[] x, int target) {
for(int i=0; i < x.length; i++) {
if (x[i]==target)
return i;
}
return -1; // target not found
}
• If the target is not present, the for loop will execute x.length times
• If the target is present the for loop will execute (on average) (x.length + 1)/2 times
• Therefore, the total execution time is directly proportional to x.length
• This is described as a growth rate of order n OR
• O(n)
47
![Page 48: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/48.jpg)
n x m Growth Rate
Processing time can be dependent on two different inputs
public static boolean areDifferent(int[] x, int[] y) {
for(int i=0; i < x.length; i++) {
if (search(y, x[i]) != -1)
return false;
}
return true;
}
48
![Page 49: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/49.jpg)
n x m Growth Rate (cont.)
public static boolean areDifferent(int[] x, int[] y) {
for(int i=0; i < x.length; i++) {
if (search(y, x[i]) != -1)
return false;
}
return true;
}
• The for loop will execute x.length times
• But it will call search, which will execute y.length times
• The total execution time is proportional to (x.length * y.length)
• The growth rate has an order of n x m or
• O(n x m)
49
![Page 50: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/50.jpg)
Quadratic Growth Rate
If n==m, thethe algorithm grows at a quadratic rate
public static boolean areUnique(int[] x) {
for(int i=0; i < x.length; i++) {
for(int j=0; j < x.length; j++) {
if (i != j && x[i] == x[j])
return false;
}
}
return true;
}
50
![Page 51: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/51.jpg)
Quadratic Growth Rate (cont.)
public static boolean areUnique(int[] x) {
for(int i=0; i < x.length; i++) {
for(int j=0; j < x.length; j++) {
if (i != j && x[i] == x[j])
return false;
}
}
return true;
}
• The for loop with i as index will execute x.length times
• The for loop with j as index will execute x.length times
• The total number of times the inner loop will execute is (x.length)2
• The growth rate has an order of n2
or• O(n2)
51
![Page 52: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/52.jpg)
Big-O Notation
order of magnitude Determined by the loops/nested loops Examples
a single loop is O(n) a pair of nested loops is O(n2) a nested pair of loops inside another is
O(n3) and so on . . .
52
![Page 53: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/53.jpg)
Logarithmic Growth Rate
Examplefor(i=1; i < x.length; i *= 2) {
// Do something with x[i]
}
‘i’ has the following values: 1, 2, 4, 8, 16, . . ., 2k
The loop body will execute k-1 times O(log n)
Logarithmic function grows slowly as the size n increases
53
![Page 54: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/54.jpg)
Example of Big-O
Consider the following program structure:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Simple Statement }
}
for (int i = 0; i < n; i++) {
Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 }
Simple Statement 6 Simple Statement 7...
Simple Statement 30
54
![Page 55: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/55.jpg)
Example of Big-O (Cont.)
Consider the following program structure:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Simple Statement }
}
for (int i = 0; i < n; i++) {
Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 }
Simple Statement 6 Simple Statement 7...
Simple Statement 30
This nested loop executes a Simple
Statement n2 times
55
![Page 56: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/56.jpg)
Example of Big-O (Cont.)
Consider the following program structure:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Simple Statement }
}
for (int i = 0; i < n; i++) {
Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 }
Simple Statement 6 Simple Statement 7...
Simple Statement 30
This loop executes 5 Simple
Statements n times (5n)
56
![Page 57: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/57.jpg)
Example of Big-O (cont.)
Consider the following program structure:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Simple Statement }
}
for (int i = 0; i < n; i++) {
Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 }
Simple Statement 6 Simple Statement 7...
Simple Statement 30
Finally, 25 Simple Statements are
executed
57
![Page 58: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/58.jpg)
Example of Big-O (cont.)
Consider the following program structure:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Simple Statement }
}
for (int i = 0; i < n; i++) {
Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 }
Simple Statement 6 Simple Statement 7...
Simple Statement 30
We can conclude that the relationship between
processing time and n (the number of date items
processed) is:
T(n) = n2 + 5n + 25
58
![Page 59: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/59.jpg)
Big-O Boundary
In terms of T(n),T(n) = O(f(n))
There exist two constants, n0 and c, greater than zero,
and a function, f(n),
such that for all n > n0, cf(n) = T(n) ≤ cf(n) as n gets sufficiently large cf(n) is an upper bound on performance
59
![Page 60: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/60.jpg)
Big-O boundary for the example
The growth rate of f(n) is determined by the highest order term
O(n2 + 5n + 25) O(n2) ignore all constants and drop the lower-order terms
60
![Page 61: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/61.jpg)
boundary for the Example
Given T(n) = n2 + 5n + 25, show that this is O(n2)
Find constants n0 and c so that, for all n > n0, cn2 > n2 + 5n + 25
When n0 is 5, and c is 3, we have 3n2 > n2 + 5n + 25 for all n > 5
61
![Page 62: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/62.jpg)
Graph representation62
![Page 63: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/63.jpg)
Big-O Example 2
Consider the following loopfor (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
3 simple statements
}
}
T(n) = 3(n – 1) + 3 (n – 2) + … + 3 Factoring out the 3,
3(n – 1 + n – 2 + n – 3 + … + 1) 1 + 2 + … + n – 1 = (n x (n-1))/2
63
![Page 64: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/64.jpg)
Big-O Example 2 (cont.)
Therefore T(n) = 1.5n2 – 1.5n When n = 0, the polynomial has the
value 0 For values of n > 1, 1.5n2 > 1.5n2 – 1.5n Therefore T(n) is O(n2) when n0 is 1 and
c is 1.5
64
![Page 65: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/65.jpg)
Big-O Example 2 (cont.)65
![Page 66: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/66.jpg)
Symbols Used in Quantifying Performance
66
![Page 67: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/67.jpg)
Common Growth Rates67
![Page 68: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/68.jpg)
Different Growth Rates68
![Page 69: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/69.jpg)
Effects of Different Growth Rates
69
![Page 70: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/70.jpg)
Exponential/Factorial Growth Rates
exponential and factorial growth rates are very sensitive to the size of the problem
With an O(2n) algorithm 100 inputs will take 1 hour 101 inputs will take 2 hours 105 inputs will take 32 hours 114 inputs will take 16,384 hours (almost 2
years!)
70
![Page 71: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/71.jpg)
Applications of Exponential/Factorial Growth Rates
Encryption algorithms take advantage of this
Some cryptographic algorithms can be broken in O(2n) time, where n is the number of bits in the key
If n==40 is considered breakable by a modern computer,
but if n==100, would take a billion-billion (1018) times longer than n==40
71
![Page 72: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/72.jpg)
Performance of KWArrayList The set and get methods execute in
constant time: O(1) Inserting or removing general elements
is linear time: O(n) Adding at the end is (usually) constant
time: O(1) With our reallocation technique the average
is O(1) The worst case is O(n) because of
reallocation
72
![Page 73: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/73.jpg)
Section 2.5
Single-Linked Lists73
![Page 74: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/74.jpg)
Single-Linked Lists
Why need Linked List? (Add, Remove Performance) ArrayList O(n) Linked List O(1)
74
![Page 75: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/75.jpg)
A List Node
A node can contain: a data item (E) one or more links
(next)
75
![Page 76: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/76.jpg)
List Nodes for Single-Linked Lists
private static class Node<E> {
private E data;
private Node<E> next;
/** Creates a new node with a null next field
@param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/** Creates a new node that references another node
@param dataItem The data stored
@param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
76
![Page 77: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/77.jpg)
List Nodes for Single-Linked Lists (cont.)
private static class Node<E> {
private E data;
private Node<E> next;
/** Creates a new node with a null next field
@param dataItem The data stored
*/
private Node(E data) {
data = dataItem;
next = null;
}
/** Creates a new node that references another node
@param dataItem The data stored
@param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
The keyword static indicates that the
Node<E> class will not reference its outer
class
Static inner classes are also called nested
classes
77
![Page 78: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/78.jpg)
List Nodes for Single-Linked Lists (cont.)
private static class Node<E> {
private E data;
private Node<E> next;
/** Creates a new node with a null next field
@param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/** Creates a new node that references another node
@param dataItem The data stored
@param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
Generally, all details of the Node class
should be private. This applies also to the data fields and
constructors.
78
![Page 79: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/79.jpg)
Creating and Connecting NodesNode<String> tom = new Node<String>("Tom");
Node<String> dick = new Node<String>("Dick");
Node<String> harry = new Node<String>("Harry");
Node<String> sam = new Node<String>("Sam");
tom.next = dick;
dick.next = harry;
harry.next = sam;
79
![Page 80: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/80.jpg)
Example of Connecting Nodes
80
![Page 81: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/81.jpg)
A Single-Linked List Class Not a single node Linked nodes How to link the nodes?
A SingleLinkedList a data field head Let head point to the first node
public class SingleLinkedList<E> {
private Node<E> head = null;
private int size = 0;
...
}
81
![Page 82: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/82.jpg)
SLList: An Example List
head =
SLList<String>
next =data = "Tom"
Node<String>
next =data = "Dick"
Node<String>
82
![Page 83: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/83.jpg)
Implementing idea:SLList.addFirst(E item)
head =
SLList<String>
next =data = "Tom"
Node<String>
next =data = "Dick"
Node<String>
next =data = "Ann"
Node<String>
The element added to the list
83
![Page 84: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/84.jpg)
Code SLList.addFirst(E item)
private void addFirst (E item) {
Node<E> temp = new Node<E>(item, head);
head = temp;
size++;
}
or, more simply ...
private void addFirst (E item) {
head = new Node<E>(item, head);
size++;
}
This works even if head is null
84
![Page 85: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/85.jpg)
Implementing Idea addAfter(Node<E> node, E item)
head =
SLList<String>
next =data = "Tom"
Node<String>
next =data = "Dick"
Node<String>
next =data = "Ann"
Node<String>
The element added to the list
85
![Page 86: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/86.jpg)
Implementing Code: addAfter(Node<E> node, E item)
private void addAfter (Node<E> node, E item) {
Node<E> temp = new Node<E>(item, node.next);
node.next = temp;
size++;
}
or, more simply ...
private void addAfter (Node<E> node, E item) {
node.next = new Node<E>(item, node.next);
size++;
}
We declare this method private since it should not be called from outside the
class. Later we will see how this method is used to implement the public add
methods.
86
![Page 87: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/87.jpg)
Implementing Idea removeAfter(Node<E> node)
head =
SLList<String>
next =data = "Tom"
Node<String>
next =data = "Dick"
Node<String>
next =data = "Ann"
Node<String>
temp
The Node parameter
87
![Page 88: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/88.jpg)
Implementing Code:removeAfter(Node<E> node)
private E removeAfter (Node<E> node) { Node<E> temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; }}
88
![Page 89: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/89.jpg)
Implementing Idea: SLList.removeFirst()
head =
SLList<String>
next =data = "Tom"
Node<String>
next =data = "Dick"
Node<String>
temp
89
![Page 90: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/90.jpg)
Implementing Code: SLList.removeFirst()
private E removeFirst () {
Node<E> temp = head;
if (head != null) {
head = head.next;
}
if (temp != null) {
size--;
return temp.data
} else {
return null;
}
}
90
![Page 91: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/91.jpg)
Single-Linked List Traversing Idea
head =
SLList<String>
next =data = "Tom"
Node<String>
next =data = "Dick"
Node<String>
next =data = "Ann"
null
Node<String>
nodeRef
Do something
with nodeRef
Do something
with nodeRef
Do something
with nodeRef
91
![Page 92: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/92.jpg)
Single-Linked List Traversing Code
toString()can be implemented with a traversal:
public String toString() {
Node<String> nodeRef = head;
StringBuilder result = new StringBuilder();
while (nodeRef != null) {
result.append(nodeRef.data);
if (nodeRef.next != null) {
result.append(" ==> ");
}
nodeRef = nodeRef.next;
}
return result.toString();
}
92
![Page 93: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/93.jpg)
SLList.getNode(int)
private Node<E> getNode(int index) {
Node<E> node = head;
for (int i=0; i<index && node != null; i++) {
node = node.next;
}
return node;
}
93
![Page 94: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/94.jpg)
SingleLinkedList ClassMore Methods
94
![Page 95: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/95.jpg)
public E get(int index)
public E get (int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node = getNode(index);
return node.data;
}
95
![Page 96: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/96.jpg)
public E set(int index, E newValue)
public E set (int index, E anEntry) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node = getNode(index);
E result = node.data;
node.data = anEntry;
return result;
}
96
![Page 97: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/97.jpg)
public void add(int index, E item)
public void add (int index, E item) {
if (index < 0 || index > size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) {
addFirst(item);
} else {
Node<E> node = getNode(index-1);
addAfter(node, item);
}
}
97
![Page 98: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/98.jpg)
public boolean add(E item)
To add an item to the end of the list
public boolean add (E item) {
add(size, item);
return true;
}
98
![Page 99: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/99.jpg)
Section 2.6
Double-Linked Lists and Circular Lists
99
![Page 100: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/100.jpg)
Limitations of Single-Linked Lists Insertion
front is O(1) other positions is O(n)
Removing requires a reference to the previous node
Traversing Only in one direction
What is the solution ?
100
![Page 101: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/101.jpg)
Double-Linked Lists Example
101
![Page 102: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/102.jpg)
Node Class Definition
private static class Node<E> {
private E data;
private Node<E> next = null;
private Node<E> prev = null;
private Node(E dataItem) {
data = dataItem;
}
}
102
![Page 103: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/103.jpg)
Inserting into a Double-Linked List
next = = prevdata = "Harry"
Node
next = null = prevdata = "Sam"
Node
next = = prevdata = "Sharon"
Node
Node<E> sharon = new Node<E>("Sharon");
sharon.next = sam;
sharon.prev = sam.prev;
sam.prev.next = sharon;
sam.prev = sharon
from predecessor
to predecessor
sam
sharon
103
![Page 104: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/104.jpg)
Removing from a Double-Linked List
next = = prevdata = "Dick"
Node
next = = prevdata = "Harry"
Node
next = = prevdata = "Sharon"
Nodeharry
harry.prev.next = harry.nextharry.next.prev = harry.prev
104
![Page 105: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/105.jpg)
A Double-Linked List Class
Data fields: head (a reference to the first list Node) tail (a reference to the last list Node) size
Good news Insertion at either end is O(1);
Bad news insertion elsewhere is still O(n) or O(n/2)
105
![Page 106: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/106.jpg)
Circular Lists
Double-linked circular list: Link last node to the first node, and Link first node to the last node
Single-linked circular lists: Traverse in forward direction only
Advantages: Circular Traverse from node
Disadvantage: Infinite loop!
106
![Page 107: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/107.jpg)
Circular Lists Example107
![Page 108: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/108.jpg)
Section 2.7
The LinkedList Class and the Iterator, ListIterator, and Iterable Interfaces
108
![Page 109: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/109.jpg)
The LinkedList Class: Methods
109
![Page 110: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/110.jpg)
The Iterator
Moving place marker keeps track of the current position in a
linked list Starts from the first node Move the Iterator
next method An Iterator traversing performance
O(n) vs. O(n2) (?) with get() of a list
110
![Page 111: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/111.jpg)
Iterator Interface
Defined in java.util ‘Iterator’ Method is Declared in the List
interface
111
![Page 112: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/112.jpg)
Iterator idea
Conceptually between elements Not refer to any particular object
112
![Page 113: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/113.jpg)
Using Iterator
Traverse a List<Integer>
Iterator<Integer> iter = aList.iterator();
while (iter.hasNext()) {
int value = iter.next();
// Do something with value
...
}
113
![Page 114: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/114.jpg)
Remove Elements with Iterator
Use Iterator remove() remove() deletes the most recent element
returned next()must be called before each remove();
OtherwiseIllegalStateException LinkedList.remove vs. Iterator.remove:
LinkedList.remove O(n2) (?) Iterator.remove O(n)
114
![Page 115: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/115.jpg)
‘Remove’ Example
remove certain elements
public static void removeDivisibleBy(LinkedList<Integer> aList, int div) {
Iterator<Integer> iter = aList.iterator();
while (iter.hasNext()) {
int nextInt = iter.next();
if (nextInt % div == 0) {
iter.remove();
}
}
}
115
![Page 116: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/116.jpg)
Iterator Limitations
Iterator limitations One direction ‘Traverse’ No add method Hard to start from a certain position of a list
Solution Using ListIterator
116
![Page 117: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/117.jpg)
ListIterator Interface
Conceptually positioned between elements of the list
Positions are indexed from 0 to size
117
![Page 118: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/118.jpg)
ListIterator Interface: Methods
118
![Page 119: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/119.jpg)
ListIterator Interface (cont.)
119
![Page 120: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/120.jpg)
Iterator vs. ListIterator
ListIterator sub-interface of Iterator
Iterator/ListIterator fewer methods/more general data structures/list only
120
![Page 121: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/121.jpg)
ListIterator and an Index
ListIterator: nextIndex()returns the index of item to be
returned by next() previousIndex() returns the index of item to
be returned by previous() listIterator(int index)
Returns a ListIterator positioned before index term
121
![Page 122: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/122.jpg)
Cont.
The listIterator (int index) creates a new ListIterator with O(n)
operation
122
![Page 123: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/123.jpg)
Enhanced for Statement
Supported from Java 5.0 The enhanced for statement
creates an Iterator object implicitly calls its hasNext and next methods Other methods, such as “remove”, not
available
123
![Page 124: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/124.jpg)
Enhanced for Example-1
Apply to a linked list
count = 0;
for (String nextStr : myList) {
if (target.equals(nextStr)) {
count++;
}
}
124
![Page 125: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/125.jpg)
Enhanced for Example-2
Apply to a linked list
sum = 0;
for (int nextInt : myList) {
sum += nextInt;
}
125
![Page 126: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/126.jpg)
Enhanced for Example-3
Apply to Array
for (char nextCh : chars) {
System.out.println(nextCh);
}
126
![Page 127: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/127.jpg)
Iterable Interface
Iterable Collection List
Iterator() declared in Iterable interface
public interface Iterable<E> {
/** returns an iterator over the elements in this collection. */
Iterator<E> iterator();
}
127
![Page 128: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/128.jpg)
Section 2.8 (Optional)
Implementation of a Double-Linked List Class
128
![Page 129: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/129.jpg)
KWLinkedList
We will define a KWLinkedList class which implements some of the methods of the List interface
The KWLinkedList class is for demonstration purposes only; Java provides a standard LinkedList class in java.util which you should use in your programs
129
![Page 130: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/130.jpg)
KWLinkedList (cont.)
import java.util.*;
/** Class KWLinkedList implements a double linked list and
* a ListIterator. */
public class KWLinkedList <E> {
// Data Fields
private Node <E> head = null;
private Node <E> tail = null;
private int size = 0;
. . .
130
![Page 131: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/131.jpg)
Add Method
1. Obtain a reference, nodeRef, to the node at position index
2. Insert a new Node containing obj before the node referenced by nodeRef
To use a ListIterator object to implement add:
3. Obtain an iterator that is positioned just before the Node at position index
4. Insert a new Node containing obj before the Node currently referenced by this iterator
/** Add an item at the specified index.
@param index The index at which the object is to be inserted
@param obj The object to be inserted
@throws IndexOutOfBoundsException if the index is out of range (i < 0 || i > size())
*/
public void add(int index, E obj) {
listIterator(index).add(obj);
}
It is not necessary to declare a local ListIterator; the
method call listIterator returns an
anonymous listIterator object
131
![Page 132: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/132.jpg)
Get Method
1. Obtain a reference, nodeRef, to the node at position index
2. Return the contents of the Node referenced by nodeRef
/** Get the element at position index.
@param index Position of item to be retrieved
@return The item at index
*/
public E get(int index) {
return listIterator(index).next();
}
132
![Page 133: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/133.jpg)
Other Add and Get Methods
public void addFirst(E item) {
add(0, item);
}
public void addLast(E item) {
add(size, item);
}
public E getFirst() {
return head.data;
}
public E getLast() {
return tail.data;
}
133
![Page 134: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/134.jpg)
Implementing the ListIterator
Interface
KWListIter is an inner class of KWLinkedList which implements the ListIterator interface
134
![Page 135: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/135.jpg)
Implementing the ListIterator
Interface (cont.)135
![Page 136: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/136.jpg)
Implementing the ListIterator
Interface (cont.)
private class KWListIter implements ListIterator<E> {
private Node <E> nextItem;
private Node <E> lastItemReturned;
private int index = 0;
...
136
![Page 137: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/137.jpg)
Constructor
public KWListIter(int i) {
// Validate i parameter.
if (i < 0 || i > size) {
throw new IndexOutOfBoundsException("Invalid index " + i);
}
lastItemReturned = null; // No item returned yet.
// Special case of last item
if (i == size) {
index = size;
nextItem = null;
}
else { // Start at the beginning
nextItem = head;
for (index = 0; index < i; index++) {
nextItem = nextItem.next;
}
}
}
137
![Page 138: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/138.jpg)
The hasNext()Method
tests to see if nextItem is nullpublic boolean hasnext() {
return nextItem != null;
}
138
![Page 139: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/139.jpg)
Advancing the Iterator
KWLinkedList
head tail size 3
next prev nulldata "Tom"
Node
next prev
Node
next prevdata "Sam"
Node
public E next() { if (!hasNext()) { throw new NoSuchElementException(); } lastItemReturned = nextItem; nextItem = nextItem.next; index++; return lastItemReturned.data;}
KWListIter
nextItem lastItemReturned index 12
data "Harry"
139
![Page 140: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/140.jpg)
Previous Methods
public boolean hasPrevious() {
return (nextItem == null && size != 0) || nextItem.prev != null;
}
public E previous() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
if (nextItem == null) { // Iterator past the last element
nextItem = tail;
}
else {
nextItem = nextItem.prev;
}
lastItemReturned = nextItem;
index--;
return lastItemReturned.data;
}
140
![Page 141: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/141.jpg)
The Add Method
When adding, there are four cases to address: Add to an empty list Add to the head of the list Add to the tail of the list Add to the middle of the list
141
![Page 142: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/142.jpg)
Adding to an Empty List
if (head == null) { head = new Node<E>(obj); tail = head;}...size++
(after insertion)
142
![Page 143: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/143.jpg)
Adding to the Head of the List
Node
next =null = prevdata = "Tom"
KWListIter
nextItem = lastItemReturned = null index = 0
if (nextItem == head) { Node<E> newNode = new Node<E>(obj); newNode.next = nextItem; nextItem.prev = newNode; head = newNode;}...size++;index++;
KWLinkedList
head = nulltail = nullsize = 3
1Node
next = = prevdata = "Harry"
Node
next = null = prevdata = "Sam"
4
next = nullnull = prevdata = "Ann"
Node
newNode
143
![Page 144: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/144.jpg)
Adding to the Tail of the List
Node
next =prev = nulldata = "Tom"
KWListIter
nextItem = nulllastItemReturned = null index = 2
if (nextItem == null) { Node<E> newNode = new Node<E>(obj); tail.next = newNode; newNode.prev = tail; tail = newNode}...size++;index++;
KWLinkedList
head = nulltail = nullsize = 3
3 Node
next = = prevdata = "Ann"
Node
next = null = prevdata = "Sam"
4
next = nullnull = prevdata = "Bob"
Node
newNode
144
![Page 145: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/145.jpg)
Adding to the Middle of the List
Node
next =prev = nulldata = "Tom"
KWListIter
nextItem = nulllastItemReturned = null index = 1
else { Node<E> newNode = new Node<E>(obj); newNode.prev = nextItem.prev; nextItem.prev.next = newNode; newNode.next = nextItem; nextItem.prev = newNode;}...size++;index++;
KWLinkedList
head = nulltail = nullsize = 3
2 Node
next = = prevdata = "Ann"
Node
next = null = prevdata = "Sam"
4
next = nullnull = prevdata = "Bob"
Node
newNode
145
![Page 146: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/146.jpg)
Inner Classes: Static and Nonstatic
KWLinkedList contains two inner classes: Node<E> is declared static: there is no need for it to
access the data fields of its parent class, KWLinkedList KWListIter cannot be declared static because its
methods access and modify data fields of KWLinkedList’s parent object which created it
An inner class which is not static contains an implicit reference to its parent object and can reference the fields of its parent object
Since its parent class is already defined with the parament <E>, KWListIter cannot be declared as KWListIter<E>; if it were, an incompatible types syntax error would occur
146
![Page 147: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/147.jpg)
Section 2.9
The Collections Framework Design147
![Page 148: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/148.jpg)
The Collection Interface
excluding add(int, E) get(int) remove(int) set(int, E)
but including add(E) remove(Object) the iterator method
148
![Page 149: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/149.jpg)
The Collection Framework
149<<interface
>>Iterable<
<<interface>>
Collection
<<interface>>
Queue<
<<interface>>
List<
AbstractCollection
<<interface>>Set
<<interface>>
DequeAbstractList AbstractSet
<<interface>>
SortedSet
AbstractSequentialList
Vector ArrayList HashSet
<<interface>>
NavigableSet<
TreeSet
ConcurrentSkipListSet
LinkedList StackLinkedHashS
et
![Page 150: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/150.jpg)
Common Features of Collections grow as needed hold references to objects at least two constructors
one to create an empty collection one to make a copy of another collection
150
![Page 151: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/151.jpg)
Common Methods of Collections
general Collection the order of elements is not specified
Collections implementing the List interface the order of the elements is determined by the index
151
![Page 152: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/152.jpg)
Common Features of Collections
For a general Collection the position where an object is inserted is not specified
But for ArrayList and LinkedList add(E) always inserts at the end and always returns
true
152
![Page 153: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/153.jpg)
AbstractCollection, AbstractList, and AbstractSequentialList
"helper" abstract classes help to build implementations of their corresponding
interfaces
Providing implementations for interface methods not used the helper classes require the programmer to extend the AbstractCollection class and
implement only the desired methods
153
![Page 154: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/154.jpg)
Implementing Collection<E> interface
Extend AbstractCollection<E>, which implements most operations
You need to implement only: add(E) size() iterator() an inner class that implements Iterator<E>
154
![Page 155: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/155.jpg)
Implementing a List<E> interface
Extend AbstractList<E> You need to implement only:
add(int, E) get(int) remove(int) set(int, E) size()
AbstractList implements Iterator<E> using the index
155
![Page 156: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/156.jpg)
AbstractCollection, AbstractList, and AbstractSequentialList
Another more complete way to declare KWArrayList is:
public class KWArrayList<E> extends AbstractList<E> implements List<E>
Another more complete, way to declare KWLinkedLinkedList is:
public class KWLinkedList<E> extends AbstractSequentialList<E> implements List<E>
156
![Page 157: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/157.jpg)
List and RandomAccess Interfaces
Accessing a LinkedList using an index O(n) traversal of the list until the index is
located The RandomAccess interface
Marker interface, no methods An algorithm design
Index list--OK If not, --- Optimize it
157
![Page 158: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/158.jpg)
Section 2.10
Application of the LinkedList Class158
![Page 159: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/159.jpg)
An Application: Ordered Lists
Task: Maintain a list of names in alphabetical order
Approach Develop an OrderedList class Implement a Comparable interface by providing
a compareTo(E) method Use a LinkedList class as a component of the OrderedList if OrderedList extended LinkedList, the user
could use LinkedList's add methods to add an element out of order
159
![Page 160: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/160.jpg)
Class Diagram for OrderedList
160
![Page 161: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/161.jpg)
Design161
![Page 162: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/162.jpg)
Inserting into an OrderedList Strategy for inserting new element e:
Find first item > e Insert e before that item
Refined with an iterator: Create ListIterator that starts at the
beginning of the list While the ListIterator is not at the end of the
list and e >= the next item Advance the ListIterator
Insert e before the current ListIterator position
162
![Page 163: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/163.jpg)
Inserting Diagrammed163
![Page 164: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/164.jpg)
Inserting Diagrammed (cont.)
164
![Page 165: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/165.jpg)
OrderedList.add
public void add (E e) {
ListIterator<E> iter = theList.listIterator();
while (iter.hasNext()) {
if (e.compareTo(iter.next()) < 0) {
// found element > new one
iter.previous(); // back up by one
iter.add(e); // add new one
return; // done
}
}
iter.add(e); // will add at end
}
165
![Page 166: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/166.jpg)
Using Delegation to Implement the Other Methods
public E get (int index) {
return theList.get(index);
}
public int size () {
return theList.size();
}
public E remove (E e) {
return theList.remove(e);
}
// returns an iterator positioned before the first element
public Iterator iterator() {
return theList.iterator();
}
166
![Page 167: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/167.jpg)
Section 2.11 (on yourself)
Testing167
![Page 168: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/168.jpg)
Testing
Testing runs a program or part of a program under controlled conditions to verify that results are as expected
Testing detects program defects after the program compiles (all syntax error have been removed)
While extremely useful, testing cannot detect the absence of all defects in complex programs
168
![Page 169: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/169.jpg)
Testing Levels
Unit testing: tests the smallest testable piece of the software, often a class or a sufficiently complex method
Integration testing: tests integration among units
System testing: tests the whole program in the context in which it will be used
Acceptance testing: system testing designed to show that a program meets its functional requirements
169
![Page 170: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/170.jpg)
Types of Testing
Black-box testing: tests the item (method, class, or program)
based on its interfaces and functional requirements
is also called closed-box or functional testing
is accomplished by varying input parameters across the allowed range and outside the allowed range, and comparing with independently calculated results
170
![Page 171: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/171.jpg)
Types of Testing (cont.)
White-box testing: tests the item (method, class, or program) with
knowledge of its internal structure is also called glass-box, open-box, or coverage
testing exercises as many paths through the element as
possible provides appropriate coverage
statement – ensures each statement is executed at least once
branch – ensures each choice of branch (if , switch, loops) is taken
path – tests each path through a method
171
![Page 172: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/172.jpg)
Preparations for Testing
A test plan should be developed early in the design stage—the earlier an error is detected, the easier and less expensive it is to correct it
Aspects of test plans include deciding: how the software will be tested when the tests will occur who will do the testing what test data will be used
172
![Page 173: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/173.jpg)
Testing Tips for Program Systems1. Carefully document method operation,
parameter, and class attributes using comments; follow Javadoc conventions
2. Leave a trace of execution by displaying the method name as you enter it
3. Display values of all input parameters upon entering a method and values of any class attributes accessed by the method
4. Display values of all method outputs after returning from a method, together with any class attributes that are modified by a method
173
![Page 174: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/174.jpg)
Testing Tips for Program Systems (cont.)
An efficient way to display values of parameters, return values, and class attributes:
private static final boolean TESTING = true; // or false to // disable
if (TESTING) {
// Code for output statements
}
Remove these features when you are satisfied with the testing results
You can define different boolean flags for different tests
174
![Page 175: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/175.jpg)
Developing the Test Data
In black-box testing, test data should check for all expected inputs as well as unanticipated data
In white-box testing, test data should be designed to ensure all combinations of paths through the code are executed
175
![Page 176: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/176.jpg)
Testing Boundary Conditions Example
for (int i = 0; i < x.length; i++) {
if (x[i] == target)
return i;
}
Test the boundary conditions (for white-box and black-box testing) when target is: first element (x[0] == target is true) last element (x[length-1] == target is true) not in array (x[i] == target is always false) present multiple times (x[i] == target for more
than one value of i)
176
![Page 177: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/177.jpg)
Testing Boundary Conditions (cont.)
for (int i = 0; i < x.length; i++) {
if (x[i] == target)
return i;
}
Test for the typical situation when target is: somewhere in the middle
and for the boundary conditions when the array has only one element no elements
177
![Page 178: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/178.jpg)
Testing Boundary Conditions (cont.)
public static void main(String[] args) {
int[] x = {5, 12, 15, 4, 8, 12, 7}; // Array to search.
// Test for target as first element.
verify(x, 5, 0);
// Test for target as last element.
verify(x, 7, 6);
// Test for target not in array.
verify(x, -5, -1);
// Test for multiple occurrences of target.
verify(x, 12, 1);
// Test for target somewhere in middle.
verify(x, 4, 3);
// Test for 1-element array.
x = new int[1];
x[0] = 10;
verify(x, 10, 0);
verify(x, -10, -1);
// Test for an empty array.
x = new int[0];
verify(x, 10, -1);
}
178
![Page 179: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/179.jpg)
Testing Boundary Conditions (cont.)
private static void verify(int[] x, int target, int expected) {
int actual = search(x, target);
System.out.print("search(x, " + target + ") is "
+ actual + ", expected " + expected);
if (actual == expected)
System.out.println(": Pass");
else
System.out.println(": ****Fail");
}
179
![Page 180: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/180.jpg)
Testing Boundary Conditions (cont.)180
![Page 181: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/181.jpg)
Stubs
Stubs are method placeholders for methods called by other classes, but not yet implemented
Stubs allowing testing as classes are being developed
A sample stub:public void save() {
System.out.println("Stub for save has been called");
modified = false;
}
181
![Page 182: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/182.jpg)
Stubs (cont.)
Stubs can print out value of inputs assign predictable values to outputs change the state of variables
182
![Page 183: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/183.jpg)
Preconditions and Postconditions
A precondition is a statement of any assumptions or constraints on the input parameters before a method begins execution
A postcondition describes the result of executing the method, including any change to the object’s state
A method's preconditions and postconditions serve as a contract between a method caller and the method programmer
/** Method Save
pre: the initial directory contents are read from a data file
post: writes the directory contents back to a data file
*/
public void save() {
. . .
}
183
![Page 184: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/184.jpg)
Drivers
Another testing tool A driver program
declares any necessary object instances and variables
assigns values to any of the method's inputs (specified by the preconditions)
calls the method displays the outputs returned by the method
Driver program code can be added to a class's main method (each class can have a main method; only one main method - the one you designate to execute - will run)
184
![Page 185: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/185.jpg)
Finally
JUnit, a popular program for Java projects, helps you develop testing programs (see Appendix C)
Many IDEs are shipped with debugger programs you can use for testing
185
![Page 186: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/186.jpg)
Testing OrderedList
To test an OrderedList, store a collection of randomly generated integers in
an OrderedList test insertion at beginning of list: insert a negative
integer test insertion at end of list: insert an integer larger
than any integer in the list create an iterator and iterate through the list,
displaying an error if any element is smaller than the previous element
remove the first element, the last element, and a middle element, then traverse to show that order is maintained
186
![Page 187: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/187.jpg)
Testing OrderedList (cont.)
Class TestOrderedList
import java.util.*;
public class TestOrderedList { /** Traverses ordered list and displays each element.
Displays an error message if an element is out of order.@param testList An ordered list of integers
*/ public static void traverseAndShow(OrderedList<Integer> testList) {
int prevItem = testList.get(0);
// Traverse ordered list and display any value that// is out of order.for (int thisItem : testList) { System.out.println(thisItem);
if (prevItem > thisItem)System.out.println("*** FAILED, value is " + thisItem);
prevItem = thisItem;}
}
public static void main(String[] args) {OrderedList<Integer> testList = new OrderedList<Integer>();final int MAX_INT = 500;final int START_SIZE = 100;
(cont. on next slide)
187
![Page 188: CHAPTER 2 Lists and the Collections Framework. Chapter Objectives The List interface Writing an array-based implementation of List Linked list data structures:](https://reader036.vdocuments.site/reader036/viewer/2022081516/5518d28255034638098b5145/html5/thumbnails/188.jpg)
Testing OrderedList (cont.)
// Create a random number generator.Random random = new Random();for (int i = 0; i < START_SIZE; i++) { int anInteger = random.nextInt(MAX_INT); testList.add(anInteger);}
// Add to beginning and end of list.testList.add(-1);testList.add(MAX_INT + 1);traverseAndShow(testList); // Traverse and display.
// Remove first, last, and middle elements.Integer first = testList.get(0);Integer last = testList.get(testList.size() - 1);Integer middle = testList.get(testList.size() / 2);testList.remove(first);testList.remove(last);testList.remove(middle);traverseAndShow(testList); // Traverse and display.
}}
188