Date post: | 30-Mar-2015 |
Category: |
Documents |
Upload: | jarred-varvel |
View: | 212 times |
Download: | 0 times |
COMP 121COMP 121
Week 9: AbstractList and Week 9: AbstractList and ArrayListArrayList
ObjectivesObjectives
List common operations and properties of List common operations and properties of Lists as distinct from CollectionsLists as distinct from CollectionsExtend the Extend the AbstractCollectionAbstractCollection implementation into implementation into AbstractListAbstractList and and ArrayListArrayList implementations and justify implementations and justify design decisionsdesign decisionsAnalyze the Analyze the ArrayListArrayList implementation to implementation to determine algorithmic efficiencydetermine algorithmic efficiencyUse Use ArrayListArrayList to solve a problem to solve a problem
ListList Interface Interface
A A ListList is an expandable collection of elements in which is an expandable collection of elements in which each element has a position or indexeach element has a position or indexSome lists allow random accessSome lists allow random accessOther lists allow sequential accessOther lists allow sequential accessAllowed operations on the Allowed operations on the ListList interface include: interface include: Finding a specified targetFinding a specified target Adding an element to either endAdding an element to either end Removing an item from either endRemoving an item from either end Traversing the list structure without a subscriptTraversing the list structure without a subscript
Not all classes perform the allowed operations with the Not all classes perform the allowed operations with the same degree of efficiencysame degree of efficiencyListList classes provide the ability to store references to classes provide the ability to store references to ObjectsObjects
Our Our ListList Interface Methods Interface Methods
public interface List<E> extends Collection<E>public interface List<E> extends Collection<E>
{{
void add(int index, E element);void add(int index, E element);
boolean addAll(int index, Collection<? extends E> coll);boolean addAll(int index, Collection<? extends E> 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<E> listIterator();ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);ListIterator<E> listIterator(int index);
E remove(int index);E remove(int index);
E set(int index, E element);E set(int index, E element);
List<E> subList(int fromIndex, int toIndex);List<E> subList(int fromIndex, int toIndex);
}}
ListList Class Hierarchy Class Hierarchy
ArrayListArrayList
Simplest class that implements the Simplest class that implements the ListList interface interfaceImprovement over an array (fixed-size data structure)Improvement over an array (fixed-size data structure)Used when a programmer wants to add new elements to Used when a programmer wants to add new elements to the end of a list, but still needs the capability to access the end of a list, but still needs the capability to access the elements stored in the list in arbitrary orderthe elements stored in the list in arbitrary orderThe size of an ArrayList automatically increases as new The size of an ArrayList automatically increases as new elements are addedelements are added The size method returns the current sizeThe size method returns the current size
The capacity of an ArrayList is the number of elements The capacity of an ArrayList is the number of elements an ArrayList can store (grows automatically!)an ArrayList can store (grows automatically!)
ArrayList ArrayList (cont’d)(cont’d)
The add method can:The add method can: Add an element at the end of the Add an element at the end of the ArrayListArrayList
myList.add(“Bashful”);myList.add(“Bashful”);
myList.add(“Awful”);myList.add(“Awful”);
myList.add(“Jumpy”);myList.add(“Jumpy”);
myList.add(“Happy”);myList.add(“Happy”);
Add an element at a specific subscript positionAdd an element at a specific subscript positionmyList.add(2,”Doc”);myList.add(2,”Doc”);
Subsequent calls to the add method will add at the Subsequent calls to the add method will add at the end of the end of the ArrayListArrayListmyList.add(“Dopey”);myList.add(“Dopey”);
ArrayList ArrayList (cont’d)(cont’d)
ArrayList ArrayList (cont’d)(cont’d)
The remove method can:The remove method can: Remove an element at a certain subscript position:Remove an element at a certain subscript position:
myList.remove(1);myList.remove(1);
You cannot access an You cannot access an ArrayListArrayList element element directly using a subscript (like with the array), but directly using a subscript (like with the array), but you can use the get/set methodsyou can use the get/set methods
String dwarf = myList.get(2);String dwarf = myList.get(2);myList.set(2,”Sneezy”);myList.set(2,”Sneezy”);
You can search an You can search an ArrayListArrayList for an element for an elementmyList.indexOf(“Sneezy”);myList.indexOf(“Sneezy”);
Will return a -1 if the element is not foundWill return a -1 if the element is not found
ArrayList ArrayList (cont’d)(cont’d)
Iterator<E>Iterator<E> Interface Interface
Defined as part of Defined as part of java.utiljava.util package package
ListList interface declares the method called interface declares the method called iteratoriterator, which returns an , which returns an IteratorIterator object that will iterate over the elements of object that will iterate over the elements of that listthat list
An An IteratorIterator does not refer to or point to does not refer to or point to a particular node at any given time but a particular node at any given time but points between nodespoints between nodes
Iterator<E>Iterator<E> Interface (cont’d) Interface (cont’d)
IterableIterable Interface Interface
This interface requires only that a class that This interface requires only that a class that implements it provide an implements it provide an iterator()iterator() method method
The The CollectionCollection interface extends the interface extends the IterableIterable interface, so all classes that interface, so all classes that implement the implement the ListList interface (a subinterface of interface (a subinterface of CollectionCollection) must provide an ) must provide an iterator()iterator() methodmethod
The Enhanced The Enhanced forfor Statement Statement
SummarySummary
The The ListList interface defines an expandable interface defines an expandable collection of elements in which each element collection of elements in which each element has a position or indexhas a position or indexElements in a Elements in a ListList can be accessed using an can be accessed using an indexindexThe Java API provides the The Java API provides the ArrayList<E>ArrayList<E> class, which uses an array as the underlying class, which uses an array as the underlying structure to implement the Liststructure to implement the List
Summary (cont’d)Summary (cont’d)
An An IteratorIterator provides the ability to access the provides the ability to access the items in a items in a ListList or or CollectionCollection sequentially sequentiallyThe The IteratorIterator interface defines the methods interface defines the methods available to an available to an IteratorIteratorThe The IterableIterable interface is extended by the interface is extended by the Collection interface at the root of the Collection Collection interface at the root of the Collection hierarchyhierarchyThe enhanced for statement makes it easy to The enhanced for statement makes it easy to iterate through a collection (or an array) without iterate through a collection (or an array) without explicitly using an explicitly using an IteratorIterator
Questions?Questions?
Exam Review – ModuExam Review – Module 5le 5
Read and write text filesRead and write text files
Explain the purpose and use of exception Explain the purpose and use of exception handling for error detection and correctionhandling for error detection and correction
Differentiate between checked and unchecked Differentiate between checked and unchecked exceptionsexceptions
Use the keywords Use the keywords throwsthrows, , trytry, , throwthrow, , catchcatch, , and and finallyfinally to implement exception handling to implement exception handling
Define and use a domain-specific exception Define and use a domain-specific exception hierarchyhierarchy
Exam Review – Module 6Exam Review – Module 6
Read and write binary filesRead and write binary files
Read and write serialized object filesRead and write serialized object files
Distinguish between random and sequential Distinguish between random and sequential access filesaccess files
Exam Review – Module 7Exam Review – Module 7
Compare and contrast iterative versus sequential Compare and contrast iterative versus sequential software lifecycle approachessoftware lifecycle approachesUse CRC cards to capture the responsibilities of each Use CRC cards to capture the responsibilities of each class and the relationships between classesclass and the relationships between classesUse UML class diagrams to illustrate relationships Use UML class diagrams to illustrate relationships between classesbetween classesUse primitive operation counts and deductive reasoning Use primitive operation counts and deductive reasoning to determine the efficiency of algorithmsto determine the efficiency of algorithmsGiven a set of initial conditions, predict how the runtime Given a set of initial conditions, predict how the runtime of an algorithm is affected by increased input sizeof an algorithm is affected by increased input size
Exam Review – Module 8Exam Review – Module 8
Describe how generics increase code reuse and Describe how generics increase code reuse and type-safetytype-safetyList the common operations and properties of all List the common operations and properties of all collectionscollectionsImplement the Implement the CollectionCollection interface in an interface in an AbstractCollectionAbstractCollection and and ArrayCollectionArrayCollection and justify design decisions and justify design decisionsRecognize and apply the Iterator design pattern Recognize and apply the Iterator design pattern to solve a given problemto solve a given problem