Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.

Similar presentations


Presentation on theme: "COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection."— Presentation transcript:

1 COMP 121 Week 9: AbstractList and ArrayList

2 Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection implementation into AbstractList and ArrayList implementations and justify design decisions Analyze the ArrayList implementation to determine algorithmic efficiency Use ArrayList to solve a problem

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 Finding a specified target Adding an element to either end Adding an element to either end Removing an item from either end Removing an item from either end Traversing the list structure without a subscript 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

4 Our List Interface Methods public interface List extends Collection public interface List extends Collection { void add(int index, E element); void add(int index, E element); boolean addAll(int index, Collection coll); boolean addAll(int index, Collection coll); E get (int index); E get (int index); int indexOf(Object obj); int indexOf(Object obj); int lastIndexOf(Object obj); int lastIndexOf(Object obj); ListIterator listIterator(); ListIterator listIterator(); ListIterator listIterator(int index); ListIterator listIterator(int index); E remove(int index); E remove(int index); E set(int index, E element); E set(int index, E element); List subList(int fromIndex, int toIndex); List subList(int fromIndex, int toIndex);}

5 List Class Hierarchy

6 ArrayList Simplest class that implements the List interface Improvement over an array (fixed-size data structure) 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 size method returns the current size The capacity of an ArrayList is the number of elements an ArrayList can store (grows automatically!)

7 ArrayList (contd) The add method can: Add an element at the end of the ArrayList Add an element at the end of the ArrayListmyList.add(Bashful);myList.add(Awful);myList.add(Jumpy);myList.add(Happy); Add an element at a specific subscript position Add an element at a specific subscript positionmyList.add(2,Doc); Subsequent calls to the add method will add at the end of the ArrayList Subsequent calls to the add method will add at the end of the ArrayListmyList.add(Dopey);

8 ArrayList (contd)

9 The remove method can: Remove an element at a certain subscript position: Remove an element at a certain subscript position:myList.remove(1); You cannot access an ArrayList element directly using a subscript (like with the array), 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 myList.indexOf(Sneezy); Will return a -1 if the element is not found Will return a -1 if the element is not found

10 ArrayList (contd)

11 Iterator Interface Defined as part of java.util package 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

12 Iterator Interface (contd)

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

14 The Enhanced for Statement

15 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 class, which uses an array as the underlying structure to implement the List

16 Summary (contd) 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

17 Questions?

18 Exam Review – Module 5 Read and write text files Explain the purpose and use of exception handling for error detection and correction Differentiate between checked and unchecked exceptions Use the keywords throws, try, throw, catch, and finally to implement exception handling Define and use a domain-specific exception hierarchy

19 Exam Review – Module 6 Read and write binary files Read and write serialized object files Distinguish between random and sequential access files

20 Exam Review – Module 7 Compare and contrast iterative versus sequential software lifecycle approaches Use CRC cards to capture the responsibilities of each class and the relationships between classes Use UML class diagrams to illustrate relationships between classes Use primitive operation counts and deductive reasoning to determine the efficiency of algorithms Given a set of initial conditions, predict how the runtime of an algorithm is affected by increased input size

21 Exam Review – Module 8 Describe how generics increase code reuse and type-safety List the common operations and properties of all collections Implement the Collection interface in an AbstractCollection and ArrayCollection and justify design decisions Recognize and apply the Iterator design pattern to solve a given problem


Download ppt "COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection."

Similar presentations


Ads by Google