Collections and Iterators - Department of Computer Science
Download
Report
Transcript Collections and Iterators - Department of Computer Science
COM S 228
Collections and Iterators
Instructor: Ying Cai
Department of Computer Science
Iowa State University
[email protected]
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
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 implement
Disadvantages
• 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
Iterator
FirstCollection
Cursor: the index of
the item retrieved by
next()
• hasNext()
• next()
• remove()
Array
Singly-Linked-List
Doubly-Linked-List
data
The List Interface
A 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
newNode
current
A Doubly-Linked List Implementation
ListIterator
cursor
cursor
cursor
cursor
remove() needs to know which
direction we’are coming from,
AHEAD, BEHIND, or NONE. After
it has been called, we have to set
direction to NONE, so that it does
not get called unless there’s
another 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)
Doubly-Linked-List
Array
data
Array or Doubly-Linked-List?
Doubly-Linked-List
Array
data
Doubly-Linked-List
Array
data
Doubly-Linked-List
Array
data