COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State...
-
Upload
jack-fowler -
Category
Documents
-
view
219 -
download
0
Transcript of COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State...
COM S 228Collections and Iterators
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]: 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
Factors to Considers
Factors to Considers
Java Collections Framework
Key methods of Collection<E>
Key methods of Iterator<E>
Java Collections Framework
foreach loop
AbstractCollection<E>
An Array-Based Generic Collection
Basic data structure
Constructors
complexity of adding n items?
Iterators
Iterators
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
Singly-Linked Lists
data next
We can build a list like this
Null-terminated singly-linked list
We can access any element by starting at head
Mostly, we will use a loop:
We can build a list like this
Null-terminated singly-linked list
Suppose we have this list
Now we do this
The result is
This effectively removes the node containing c
Suppose we have this list
What happens if we do this
Problems with Singly-Linked Lists
• Cannot quickly access the predecessor, making it difficult to delete an element
• Can only iterate in one direction
Doubly-Linked Lists
Iterator for Doubly-Linked Lists
cursor
cursor
FirstCollection
Iterator
Cursor: the index of the item retrieved by next()
• hasNext()• next()• remove()
Array data
Singly-Linked-List
Doubly-Linked-List
The List InterfaceA list is a linearly ordered collection with random access to the elements. The List interface extends the Collection interface:
List
Two ListIterator
Two ListIterator
We will implement the List interface on top of the Existing class AbstractSequentialList.
A Doubly-Linked List Implementation
Some helpful methods
current
newNode
A Doubly-Linked List Implementation
ListIterator
cursor
cursor
cursor
cursor
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().
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
Array or Doubly-Linked-List?
Array data
Doubly-Linked-List
Array data
Doubly-Linked-List
Array data
Doubly-Linked-List