1 / 55

Java Collections and Iterators: Storing and Accessing Elements of the Same Type

This course provides an in-depth understanding of the Java Collections Framework and its key methods. Learn how to store and access collections of elements using arrays and linked lists.

nbryan
Télécharger la présentation

Java Collections and Iterators: Storing and Accessing Elements of the Same Type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  4. Factors to Considers

  5. Java Collections Framework

  6. Key methods of Collection<E> Key methods of Iterator<E>

  7. Java Collections Framework

  8. foreach loop

  9. AbstractCollection<E>

  10. An Array-Based Generic Collection

  11. Basic data structure Constructors

  12. complexity of adding n items?

  13. Iterators

  14. Iterators

  15. Array-based Implementation of FirstCollection Data size: the index of next available slot Iterator Cursor: the index of the item retrieved by next() • hasNext() • next() • remove() checkCapacity() • 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

  16. Singly-Linked Lists data next

  17. We can build a list like this Null-terminated singly-linked list

  18. We can access any element by starting at head Mostly, we will use a loop:

  19. We can build a list like this Null-terminated singly-linked list

  20. Suppose we have this list Now we do this The result is This effectively removes the node containing c

  21. Suppose we have this list What happens if we do this

  22. Problems with Singly-Linked Lists • Cannot quickly access the predecessor, making it difficult to delete an element • Can only iterate in one direction

  23. Doubly-Linked Lists

  24. Iterator for Doubly-Linked Lists cursor

  25. cursor

  26. Iterator Cursor: the index of the item retrieved by next() FirstCollection • hasNext() • next() • remove() Array data Singly-Linked-List Doubly-Linked-List

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

  28. List

  29. Two ListIterator

  30. Two ListIterator

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

  32. A Doubly-Linked List Implementation

  33. Some helpful methods

  34. newNode current

  35. A Doubly-Linked List Implementation

  36. ListIterator

  37. cursor

  38. cursor

  39. cursor

  40. cursor

  41. 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().

More Related