Download presentation
Presentation is loading. Please wait.
Published byJack Fowler Modified over 9 years ago
1
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201
2
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
3
Factors to Considers
5
Java Collections Framework
6
Key methods of Collection Key methods of Iterator
7
Java Collections Framework
9
foreach loop
10
AbstractCollection
11
An Array-Based Generic Collection
12
Basic data structure Constructors
14
complexity of adding n items?
15
Iterators
17
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
18
Singly-Linked Lists datanext
19
We can build a list like this Null-terminated singly-linked list
20
We can access any element by starting at head Mostly, we will use a loop:
21
We can build a list like this Null-terminated singly-linked list
22
Suppose we have this list Now we do this The result is This effectively removes the node containing c
23
Suppose we have this list What happens if we do this
24
Problems with Singly-Linked Lists Cannot quickly access the predecessor, making it difficult to delete an element Can only iterate in one direction
25
Doubly-Linked Lists
27
Iterator for Doubly-Linked Lists cursor
29
FirstCollection Iterator Cursor: the index of the item retrieved by next() hasNext() next() remove() Array data Singly-Linked-List Doubly-Linked-List
30
The List Interface A list is a linearly ordered collection with random access to the elements. The List interface extends the Collection interface:
31
List
32
Two ListIterator
37
We will implement the List interface on top of the Existing class AbstractSequentialList.
38
A Doubly-Linked List Implementation
39
Some helpful methods
40
current newNode
42
A Doubly-Linked List Implementation
43
ListIterator
44
cursor
50
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().
52
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) Arraydata Doubly-Linked-List
53
Array or Doubly-Linked-List? Arraydata Doubly-Linked-List
54
Arraydata Doubly-Linked-List
55
Arraydata Doubly-Linked-List
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.