CS 146: Data Structures and Algorithms
June 9 Class Meeting
Department of Computer Science
San Jose State University
Summer 2015
Instructor: Ron Mak
www.cs.sjsu.edu/~mak
Primitive Types and Reference Types
Java has primitive types and reference types.
Primitive types are int, short, long, byte ,
float, double, char, and boolean.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
2
Primitive Types and Reference Types, cont’d
Reference types (AKA object types) are for
objects that are created with the new operator.
Java objects are always referred to by pointers.
Object is the root of all reference types
in the Java type hierarchy.
Built-in reference types include Object, Integer,
Float, Date, System, ArrayList, Hashtable
An array is a reference type.
You can define custom reference types.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
3
More on Generic Types: Type Erasure
Generic types like ArrayList<SimpleShape>
are constructs of the Java language itself.
The Java compiler does type erasure.
The Java virtual machine does not know
about generic types.
Convert generic types back to their raw types.
Replace type parameters by their bounds.
Add type casting as necessary.
There are restrictions on using generic types.
Read about them in the text book, pp. 23-24.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
4
Function Objects
We saw how SimpleShape implements
interface Comparable<SimpleShape>.
Suppose we want to write code that’s
even more generic.
Its compareTo() method compared areas.
We may want to use a variety of ways
to compare shapes.
Example: We want to compare rectangles’ heights.
We can write define function objects that wrap
each of the comparison methods.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
5
Function Objects, cont’d
Java interface Comparator in the java.util
package declares a single method compare()
that compares two objects passed in as
parameters.
The method returns a negative, zero, or positive
int value depending on whether the first object
is less than, equal to, or greater than the
second object, respectively.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
6
Function Objects, cont’d
Example function object that implements the
Comparator interface:
private static
implements
{
public int
{
return
}
}
class HeightComparator
Comparator<Rectangle>
compare(Rectangle rect1, Rectangle rect2)
(int) (rect1.getHeight() - rect2.getHeight());
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
7
Array14: Use a Function Object
public static void main(String[] args)
{
Rectangle rectangles[] = new Rectangle[] {
new Rectangle(2, 5),
new Rectangle(3, 4),
new Rectangle(1, 6)
};
System.out.println(
"Maximum height: " +
max(rectangles, new HeightComparator()).getHeight());
}
Create a HeightComparator function object
and pass it to the max() method.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
8
Array14: Use a Function Object, cont’d
private static <MyType>
MyType max(MyType elements[],
Comparator<? super MyType> comp)
{
int maxIndex = 0;
for (int i = 1; i < elements.length; i++) {
if (comp.compare(elements[i],
elements[maxIndex]) > 0)
{
maxIndex = i;
}
}
return elements[maxIndex];
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Demo
9
Scalability of Different Algorithms
LinearRuntimeGrowth:
LogarithmicRuntimeGrowth:
QuadraticRuntimeGrowth:
CubicRuntimeGrowth:
Computer Science Dept.
Summer 2015: June 9
T(N) = O(N)
T(N) = O(N log N)
T(N) = O(N2)
T(N) = O(N3)
CS 146: Data Structures and Algorithms
© R. Mak
10
How to Say “T(N) = O(f(N))”
T(N) is Big-Oh of f(N)
T(N) is order f(N)
The = sign really doesn’t mean “equals” since
there may be more than one function f(N) that
satisfies T(N) = O(f(N))
So O(f(N)) is actually a set of functions.
T(N) is in O(f(N))
T ( N ) O ( f ( N ))
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
11
Logarithms in Algorithm Analysis
An algorithm is O(log N) if it takes a constant
O(1) time to divide the problem, such as in half.
Classic example: Binary search of a sorted array.
An algorithm is O(N) if it takes a constant O(1)
time to reduce the problem by a fixed amount,
such as by 1.
Classic example: Computing factorials.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
12
Binary Search
public <AnyType extends Comparable<? super AnyType>> BinarySearch.java
int binarySearch(AnyType elements[], AnyType x, boolean flag)
{
int low = 0;
int high = elements.length - 1;
int count = 0;
while (low <= high) {
int mid = (low + high)/2;
int compare = x.compareTo(elements[mid]);
++count;
if (compare < 0) {
Search earlier part.
high = mid - 1;
}
else if (compare > 0) {
Search later part.
low = mid + 1;
}
else {
return flag ? mid : count; // found! Found!
}
}
Return either the target index or the count,
depending on the value of the boolean flag.
return flag ? NOT_FOUND
: count;
CS 146: Data Structures and Algorithms
Computer Science Dept.
}Summer 2015: June 9
© R. Mak
13
Binary Search, cont’d
Search1.java
public static void main(String[] args)
Fill the array with
{
random values from
int n = 10;
0 through 9.
BinarySearch searcher = new BinarySearch();
RandomGenerator generator = new RandomGenerator(n);
Integer intArray[] = generator.generateSortedArray(n);
for (int i = 0; i < n; i++) {
System.out.printf("%2d:%2d\n", i, intArray[i]);
}
for (int i = 0; i <= 10; i++) { Search for a random value from 0 through 9.
int target = generator.generateInt();
int index = searcher.binarySearch(intArray, target, true);
System.out.printf("Search: %2d:%2d\n", index, target);
}
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Demo
14
Binary Search, cont’d
Search2.java
private static final double LOG2 = Math.log(2);
public static void main(String[] args)
{
System.out.printf("%8s%8s%15s\n", "n", "count", "log2(n)");
for (int n = 10; n <= 100000; n*=10) {
BinarySearch searcher = new BinarySearch();
RandomGenerator generator = new RandomGenerator(100*n);
Integer intArray[] = generator.generateSortedArray(n);
int target = generator.generateInt();
int count = searcher.binarySearch(intArray, target, false);
double log = Math.log(n)/LOG2;
System.out.printf("%8d%8d%15f\n", n, count, log);
}
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Demo
15
Binary Search, cont’d
n
10
100
1000
10000
100000
1000000
count
4
7
10
14
17
20
log2(n)
3.321928
6.643856
9.965784
13.287712
16.609640
19.931569
Therefore, for the binary search algorithm,
what can we conclude about T(n)?
T(n) = Θ(log2 n)
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
16
Abstract Data Type (ADT)
A data type specifies:
An abstract data type specifies:
What kinds of data values belong in the type.
What operations are permitted on those values.
What kinds of data values belong in the type.
What operations are permitted on those values.
The implementations of the operations
of an abstract data type are hidden.
The implementations depend on the specific type.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
17
Example ADT: List
What kinds of data values?
Determined by the implementing class.
What operations?
Implemented by the implementing class:
Return the size of the list.
Get the value at a given position of the list.
Set the value at a given position of the list.
Add a value to the end of the list (append).
Add a value at a given position of the list (insert).
Remove a value at a given position of the list.
Iterate over the values in the list.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
18
Example ADT: List, cont’d
The ADT List can be implemented as:
an array
a linked list
List is an interface:
public interface List<AnyType> extends Collection<AnyType>
{
Explained later.
boolean add(AnyType element);
void add(int index, AnyType element);
AnyType get(int position);
AnyType set(int index, AnyType element);
ListIterator<AnyType> listIterator(); Explained later.
...
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
19
ADT List as an ArrayList
Advantages:
Calls to get() and set() a value
at a given position in the array are very fast
(constant O(1) time).
Disadvantages:
Calls to add() a value (insert) or to remove() a
value at a given position are expensive (O(N) time),
especially near the beginning of the array.
Data values need to be shifted to make room
for the new value (add) or to fill in the hole (remove).
If the array capacity is reached, a stop-and-copy
operation is required to increase the capacity.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
20
ADT List as a LinkedList
Advantages:
Calls to add() and remove() at any given position
in the list are very fast (constant O(1) time).
The list capacity can grow (and shrink)
at a constant O(1) time.
Disadvantages:
Accessing a value with get() or set() at a given
position in the list is expensive (O(N) time),
especially near the middle of the list.
Need to start at one end of the list and follow links
to reach the desired node.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
21
Break
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
22
Singly Linked List
Each node of the list contains a next pointer
that points to the next node in the list.
Accessing the nth value in the list means
starting at the head node and “chasing” links
to the nth node.
Adding or removing a given node is fast.
Need to have access to the preceding node
in order to modify the preceding node’s
next pointer.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
23
Singly Linked List, cont’d
Insert a new node:
Delete a node:
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Data Structures and Algorithms in Java, 3rd ed.
by Mark Allen Weiss
Pearson Education, Inc., 2012
ISBN 978-0-13-257627-7
24
Doubly Linked List
Each node of the list contains a next pointer
that points to the next node in the list and a
prev pointer that points to the previous node.
Accessing the nth value in the list means
starting at the either the head node or the
tail node, whichever is closer, and following the
next or prev links, respectively, to the nth node.
Adding or removing a node is fast.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
25
Doubly Linked List, cont’d
Use special head and tail nodes.
Eliminate special cases in the code when inserting
or deleting at the head or tail of the list.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Data Structures and Algorithms in Java, 3rd ed.
by Mark Allen Weiss
Pearson Education, Inc., 2012
ISBN 978-0-13-257627-7
26
Doubly Linked List, cont’d
Insert a new node.
Modify the pointers in the indicated order.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Data Structures and Algorithms in Java, 3rd ed.
by Mark Allen Weiss
Pearson Education, Inc., 2012
ISBN 978-0-13-257627-7
27
Doubly Linked List
Delete a node.
Modify the pointers in the indicated order.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Data Structures and Algorithms in Java, 3rd ed.
by Mark Allen Weiss
Pearson Education, Inc., 2012
ISBN 978-0-13-257627-7
28
Node Access Time: ArrayList vs. LinkedList
Access1.java
private static ArrayList<Integer> testArray;
private static LinkedList<Integer> testLinked;
private static void initLists(int n)
{
testArray = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
testArray.add(0);
}
Create an array list and
a linked listboth containing
elements with value 0.
testLinked = new LinkedList<>(testArray);
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
29
Node Access Time with List.get()
Access1.java
private static long timeElementAccess(List<Integer> lst)
{
long start = System.currentTimeMillis();
for (int i = 0; i < lst.size(); i++) {
Integer elmt = lst.get(i); Access each element successively.
}
return System.currentTimeMillis() - start;
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
30
Node Access Time with List.get(), cont’d
Access1.java
public static void main(String args[])
{
System.out.printf("%8s%15s%15s\n",
"n", "ArrayList", "LinkedList");
for (int n = 100; n <= 1000000; n*=10) {
initLists(n);
long timeArray = timeElementAccess(testArray);
long timeList = timeElementAccess(testLinked);
System.out.printf("%8d%12d ms%12d ms\n",
n, timeArray, timeList);
}
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Demo
31
Node Access Time with List.get(), cont’d
n
100
1000
10000
100000
1000000
LinkedList
0 ms
5 ms
43 ms
4208 ms
821905 ms
ArrayList
ArrayList
0 ms
0 ms
3 ms
0 ms
1 ms
Access to each element is fast.
LinkedList
Access to each element is slow due to the need to
chase links from either end of the list in order to
reach the desired element.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
32
The Java Collections Framework (JCF)
A built-in collection of common data structures.
Abstracted by the Collection interface:
public interface Collection<AnyType> extends Iterable<AnyType>
{
boolean add(AnyType element);
void clear();
boolean contains(AnyType element);
boolean equals(AnyType element);
boolean isEmpty();
boolean remove(AnyType element);
Iterator<AnyType> iterator();
...
}
Extended by interface List.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
33
The Iterable Interface
Allows you to use the special form of the
for loop to iterate over the collection elements.
for (AnyType elmt : myElements) {
...
}
No indexing or calls to get() required!
Do something with elmt inside the loop.
An iterator keeps track of the current element.
public interface Iterator<AnyType>
{
boolean hasNext();
AnyType next();
void remove();
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
34
Node Access Time with Iterator
This time, we use the special form of the
for loop to access the list elements
sequentially.
Access2.java
private static long timeElementAccess(List<Integer> lst)
{
long start = System.currentTimeMillis();
for (Integer elmt : lst) {
Integer elmt2 = elmt;
}
return System.currentTimeMillis() - start;
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Demo
35
Node Access Time with Iterator, cont’d
n
100
1000
10000
100000
1000000
LinkedList
0
0
3
0
4
ArrayList
ArrayList
1
1
5
8
1
Access to each element is fast.
LinkedList
Access to each element is fast.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
36
Node Delete Time: ArrayList vs. LinkedList
Remove1.java
private static long timeElementRemove(List<Integer> lst)
{
long start = System.currentTimeMillis();
int i = 0;
while (i < lst.size()) {
if (lst.get(i)%2 == 0) {
lst.remove(i);
Use the List.remove() method.
}
else {
i++;
}
}
return System.currentTimeMillis() - start;
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Demo
37
Node Delete Time with List.remove(), cont’d
n
100
1000
10000
100000
1000000
LinkedList
0
6
66
6700
1740173
ArrayList
ArrayList
0
0
11
401
52605
Access to each element is fast.
Each deletion is slow due to the requirement to shift
the remaining elements to fill the hole.
LinkedList
Access to each element is slow.
Each deletion is fast.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
38
Collection.remove() vs. Iterator.remove()
The Collection interface and the Iterator
interface each has a remove() method.
Collection.remove() must first access the item.
Iterator.remove() deletes its current item.
You must call next() before calling remove() again.
If you change the structure of a collection
(e.g., by adding or removing items),
the iterator becomes invalid.
The iterator will throw
ConcurrentModificationException
if you try to use the iterator after changing
the collection’s structure.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
39
Node Delete Time with Iterator.remove()
Remove2.java
private static long timeElementRemove(List<Integer> lst)
{
long start = System.currentTimeMillis();
Iterator<Integer> iter = lst.iterator();
while (iter.hasNext()) {
if (iter.next()%2 == 0) {
iter.remove(); Use the Iterator.remove() method.
}
}
return System.currentTimeMillis() - start;
}
Demo
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
40
Node Delete Time with Iterator.remove(), cont’d
n
100
1000
10000
100000
1000000
LinkedList
0
0
6
3
17
ArrayList
ArrayList
1
1
13
416
54656
Access to each element is fast.
Each deletion is slow due to the requirement to
shift the remaining elements to fill the hole.
LinkedList
Access to each element is fast.
Each deletion is fast.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
41
So Now We Know That ...
ArrayList
Access to a node at an arbitrary position is fast.
Insertions and deletions are slow.
LinkedList
Access to a node at an arbitrary position is slow.
Sequential node access using an iterator is fast.
Insertions and deletions are fast.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
42
Two Ways to Use an Iterator
Via the special form of the for loop:
for (Integer elmt : list) {
// do something with elmt
}
An iterator object:
Iterator<Integer> iter = list.iterator();
while (iter.hasNext()) {
Integer elmt = iter.next();
// do something with elmt
}
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
43
The ListIterator Interface
public Interface ListIterator<AnyType> extends Iterator<AnyType>
{
void add(AnyType element);
boolean hasNext();
boolean hasPrevious();
AnyType next();
AnyType previous();
void remove();
void set(AnyType element);
...
}
Created and returned by a list.
The “cursor” marks the current position
between nodes.
Methods remove() and set() operate on
the last node returned by next() or previous().
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
44
The ListIterator Interface, cont’d
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
Data Structures and Algorithms in Java, 3rd ed.
by Mark Allen Weiss
Pearson Education, Inc., 2012
ISBN 978-0-13-257627-7
45
Example List Implementations
Be sure to understand the example
MyArrayList and MyLinkList
implementations in the textbook.
MyArrayList<AnyType>
implements Iterable<AnyType>
MyLinkedList<AnyType>
implements Iterable<AnyType>
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
46
Java Nested Classes
Define a nested class inside its parent class:
public class ParentClass
{
...
private static class NestedClass
{
...
}
...
}
A nested class has access to all members of its
parent class, even private members.
A nested class is like a top-level (non-nested) class.
It is nested in a top-level class for better packaging.
Computer Science Dept.
Summer 2015: June 9
CS 146: Data Structures and Algorithms
© R. Mak
47
© Copyright 2026 Paperzz