1 / 18

COMP 121

Learn about the List interface and how it is implemented by the ArrayList class, as well as the Iterator and Iterable interfaces. Explore the allowed operations on the List interface and the advantages of using ArrayList class.

acarmichael
Download Presentation

COMP 121

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. COMP 121 Week 9: ArrayList

  2. Objectives • To understand the List interface • To examine how the List interface is implemented by the ArrayList class • To introduce the Iterator and Iterable interfaces • To review the enhanced for statement

  3. List Interface • A list is an expandable collection of elements in which each element has a position or index • Some lists allow random access • Other lists allow sequential access • Allowed operations on the List interface include: • Finding a specified target • Adding an element to either end • Removing an item from either end • Traversing the list structure without a subscript • Not all classes perform the allowed operations with the same degree of efficiency • List classes provide the ability to store references to Objects Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  4. List Interface and ArrayList Class Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  5. ArrayList Class • Simplest class that implements the List interface • Improvement over an array • Used when a programmer wants to add new elements to the end of a list but still needs the capability to access the elements stored in the list in arbitrary order • The size of an ArrayList automatically increases as new elements are added • The size method returns the current size • The capacity of an ArrayList is the number of elements an ArrayList can store (automatically increased) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  6. ArrayList Class (cont’d) • The add method can: • Add an element at the end of the ArrayList myList.add(“Bashful”); myList.add(“Awful”); myList.add(“Jumpy”); myList.add(“Happy”); • Add an element at a certain subscript position myList.add(2,”Doc”); • Subsequent calls to the add method will add at the end of the ArrayList myList.add(“Dopey”); Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  7. ArrayList Class (cont’d) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  8. ArrayList Class (cont’d) • The remove method can: • Remove an element at a certain subscript position myList.remove(2); • You cannot access an ArrayList element directly using a subscript, but you can use the get/set methods String dwarf = myList.get(2); myList.set(2,”Sneezy”); • You can search an ArrayList for an element • Uses the equals method myList.indexOf(“Sneezy”); • Will return a -1 if the element is not found Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  9. The ArrayList Class (cont’d) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  10. Specification of the ArrayList Class Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  11. Iterator<E> Interface • The Iterator interface is defined as part of API package java.util • The List interface declares the method called iterator, which returns an Iterator object that will iterate over the elements of that list • An Iterator does not refer to or point to a particular node at any given time but points between nodes Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  12. The Iterator<E> Interface (cont’d) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  13. Iterable Interface • This interface requires only that a class that implements it provide an iterator method • The Collection interface extends the Iterable interface, so all classes that implement the List interface (a subinterface of Collection) must provide an iterator method Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  14. The Enhanced for Statement Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  15. Enhanced for Statement • Is another way of doing … Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  16. Summary • The List interface defines an expandable collection of elements in which each element has a position or index • Elements in a List can be accessed using an index • The Java API provides the ArrayList<E> class, which uses an array as the underlying structure to implement the List Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  17. Summary (cont’d) • An iterator provides the ability to access the items in a List or Collection sequentially • The Iterator interface defines the methods available to an iterator • The Iterable interface is extended by the Collection interface at the root of the Collection hierarchy • The enhanced for statement makes it easy to iterate through a collection (or an array) without explicitly using an iterator Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  18. Any Questions?

More Related