+ All Categories
Home > Documents > COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of...

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of...

Date post: 30-Mar-2015
Category:
Upload: jarred-varvel
View: 212 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
COMP 121 COMP 121 Week 9: AbstractList Week 9: AbstractList and ArrayList and ArrayList
Transcript
Page 1: COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.

COMP 121COMP 121

Week 9: AbstractList and Week 9: AbstractList and ArrayListArrayList

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

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

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

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

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

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);

}}

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

ListList Class Hierarchy Class Hierarchy

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

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!)

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

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”);

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

ArrayList ArrayList (cont’d)(cont’d)

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

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

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

ArrayList ArrayList (cont’d)(cont’d)

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

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

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

Iterator<E>Iterator<E> Interface (cont’d) Interface (cont’d)

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

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

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

The Enhanced The Enhanced forfor Statement Statement

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

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

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

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

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

Questions?Questions?

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

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

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

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

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

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

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

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


Recommended