COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State...

55
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University [email protected] Office: Atanasoff 201

Transcript of COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State...

Page 1: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

COM S 228Collections and Iterators

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]: Atanasoff 201

Page 2: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Java Collection Framework

• Very often we need a way to store and access a collection of elements of the same type

• A few factors to consider

Page 3: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Factors to Considers

Page 4: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Factors to Considers

Page 5: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Java Collections Framework

Page 6: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Key methods of Collection<E>

Key methods of Iterator<E>

Page 7: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Java Collections Framework

Page 8: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 9: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

foreach loop

Page 10: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

AbstractCollection<E>

Page 11: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

An Array-Based Generic Collection

Page 12: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Basic data structure

Constructors

Page 13: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 14: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

complexity of adding n items?

Page 15: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Iterators

Page 16: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Iterators

Page 17: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Array-based Implementation of FirstCollection

Data size: the index of next available slot

Iterator

Cursor: the index of the item retrieved by next()

checkCapacity()

• hasNext()• next()• remove()

Advantages•Simple to implementDisadvantages•A certain amount of memory is required for the array, even when the collection contains few elements•Removing an element requires all elements down to be shifted, making it an O(n) operations

Page 18: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Singly-Linked Lists

data next

Page 19: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

We can build a list like this

Null-terminated singly-linked list

Page 20: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

We can access any element by starting at head

Mostly, we will use a loop:

Page 21: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

We can build a list like this

Null-terminated singly-linked list

Page 22: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Suppose we have this list

Now we do this

The result is

This effectively removes the node containing c

Page 23: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Suppose we have this list

What happens if we do this

Page 24: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Problems with Singly-Linked Lists

• Cannot quickly access the predecessor, making it difficult to delete an element

• Can only iterate in one direction

Page 25: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Doubly-Linked Lists

Page 26: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 27: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Iterator for Doubly-Linked Lists

cursor

Page 28: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

cursor

Page 29: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

FirstCollection

Iterator

Cursor: the index of the item retrieved by next()

• hasNext()• next()• remove()

Array data

Singly-Linked-List

Doubly-Linked-List

Page 30: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

The List InterfaceA list is a linearly ordered collection with random access to the elements. The List interface extends the Collection interface:

Page 31: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

List

Page 32: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Two ListIterator

Page 33: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Two ListIterator

Page 34: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 35: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 36: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 37: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

We will implement the List interface on top of the Existing class AbstractSequentialList.

Page 38: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

A Doubly-Linked List Implementation

Page 39: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Some helpful methods

Page 40: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

current

newNode

Page 41: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 42: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

A Doubly-Linked List Implementation

Page 43: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

ListIterator

Page 44: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

cursor

Page 45: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 46: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

cursor

Page 47: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 48: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

cursor

Page 49: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

cursor

Page 50: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

remove() needs to know whichdirection we’are coming from,AHEAD, BEHIND, or NONE. Afterit has been called, we have to setdirection to NONE, so that it doesnot get called unless there’sanother next() or previous().

Page 51: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.
Page 52: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Array-based Implementation of List A list is a linearly ordered collection with random access to the elements. The List interface extends the Collection interface:•Void add(int k, E item)•E get(int k)•E set(int pos, E element)•E remove (int k)•ListIterator() and ListIterator(int pos)

• next(), hasNext()• nextIndex(), previousIndex(), add(E item)

Array data

Doubly-Linked-List

Page 53: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Array or Doubly-Linked-List?

Array data

Doubly-Linked-List

Page 54: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Array data

Doubly-Linked-List

Page 55: COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201.

Array data

Doubly-Linked-List