List Implementation via Arrays

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Why not just use Arrays? Java ArrayLists.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
The List ADT Textbook Sections
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
(c) University of Washington14-1 CSC 143 Java Collections.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
An Array-Based Implementation of the ADT List
Iterators.
Lecture 10 Collections Richard Gesick.
EECE 310: Software Engineering
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
Lecture 15: More Iterators
Data Structures Interview / VIVA Questions and Answers
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Implementing ArrayList Part 1
Stack Data Structure, Reverse Polish Notation, Homework 7
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Programming in Java Lecture 11: ArrayList
Stacks.
Building Java Programs
Building Java Programs
ArraySet Methods and ArrayIterator
The List ADT Reading: Textbook Sections 3.1 – 3.5
slides created by Ethan Apter
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Dynamic Data Structures and Generics
Example: LinkedSet<T>
slides created by Ethan Apter
Stacks.
ArrayLists 22-Feb-19.
Lists.
Linked Lists Chapter 5 (continued)
CSC 143 Java Linked Lists.
Data Structures & Algorithms
CSC 143 Binary Search Trees.
Building Java Programs
8 List ADTs List concepts. List applications.
TCSS 143, Autumn 2004 Lecture Notes
slides created by Ethan Apter and Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
slides created by Ethan Apter
Dynamic Array: Implementation of Stack
Linked Lists Chapter 5 (continued)
Presentation transcript:

List Implementation via Arrays CSC 143 Java List Implementation via Arrays

Implementing a List in Java Two implementation approaches are most commonly used for simple lists: List via Arrays Linked list Java Interface List<E> concrete classes ArrayList, LinkedList same methods, different internals List in turn extends (implements) Collection<E> Our current activities: Lectures on list implementations, in gruesome detail MyArrayList is a class we develop as an example Projects in which lists are used

List<E> Interface (review) int size( ) boolean isEmpty( ) boolean add( E o) boolean addAll( Collection<E> other) // Not exactly the signature, but… void clear( ) E get(int pos) boolean set( int pos, E o) int indexOf( Object o) boolean contains( Object o) E remove( int pos) boolean remove( Object o) boolean add( int pos, E o) Iterator<E> iterator( )

Just an Illusion? Key concept: external view (the abstraction visible to clients) vs. internal view (the implementation) MyArrayList may present an illusion to its clients Appears to be a simple, unbounded list of elements Actually may be a complicated internal structure The programmer as illusionist... This is what abstraction is all about

Using an Array to Implement a List Idea: store the list elements in an array instance variable // Simple version of ArrayList for CSC143 lecture example public class MyArrayList<E> implements List<E> { /** variable to hold all elements of the list*/ private E[ ] elements; … } Issues: How big to make the array? Algorithms for adding and deleting elements (add and remove methods) Later: performance analysis of the algorithms elements Object[ ]

Space Management: Size vs. Capacity Idea: allocate extra space in the array, possibly more than is actually needed at a given time size: the number of elements in the list, from the client's view capacity: the length of the array (the maximum size) invariant: 0 <= size <= capacity When list object created, create an array of some initial maximum capacity What happens if we try to add more elements than the initial capacity? see later…

List Representation public class MyArrayList<E> implements List<E> { // instance variables private E[ ] elements; // elements stored in elements[0..numElems-1] private int numElems; // size: # of elements currently in the list // capacity ?? Why no capacity variable?? // default capacity private static final int DEFAULT_CAPACITY = 10; … } Review: what is the “static final”? Can do some or most of the following programming examples as in-class exercises, to explain the reasoning behind the code. The slides are then available to the students as notes for later.

Constructors We’ll provide two constructors: Review: this( … ) /** Construct new list with specified capacity */ public MyArrayList( int capacity) { this.elements = (E[]) new Object[capacity]; // new E[capacity] doesn’t work! this.numElems = 0; } /** Construct new list with default capacity */ public MyArrayList( ) { this(DEFAULT_CAPACITY); Review: this( … ) means what? can be used where? Draw pictures, of internal view (showing implementation) and corresponding external view (what the client thinks of the list as containing, i.e., the abstract view)

size, isEmpty: Signatures /** Return size of this list */ public int size( ) { } isEmpty: /** Return whether the list is empty (has no elements) */ public boolean isEmpty( ) {

size, isEmpty: Code size: isEmpty: /** Return size of this list */ public int size( ) { return this.numElems; } isEmpty: /** Return whether the list is empty (has no elements) */ public boolean isEmpty( ) { return this.size( ) == 0; //OR return this.numElems == 0; Each choice has pros and cons: what are they? A principal "pro" of this.size() == 0 is that this could be put in an abstract superclass – doens't depend on implementation, only on public methods.

Method add: simple version Assuming there is unused capacity … /** Add object o to the end of this list. @return true if the object was added successfully. This implementation always returns true. */ public boolean add(E o) { return true; } o Why does "add" return a boolean? Ans: There might be implementations where add would fail – either out of space, or because the method doesn't like the value of the argument.

Method add: simple version Assuming there is unused capacity … /** Add object o to the end of this list @return true, since list is always changed by an add */ public boolean add(E o) { if (this.numElems < this.elements.length) { this.elements[this.numElems] = o; this.numElems ++; } else { // yuck; what can we do here? here's a temporary measure…. throw new RuntimeException("list capacity exceeded"); } return true; addAll(array or list) left as an exercise – try it at home! Could your solution be put in an abstract superclass? Why does "add" return a boolean? Ans: There might be implementations where add would fail – either out of space, or because the method doesn't like the value of the argument.

Method clear: Signature /** Empty this list */ public void clear( ) { } Can be done by adding just one line of code! "Can be", but "should be"? Additional invariant being maintained (and assumed by for loop above): elements[numElems..length-1] == null But it’s probably a good idea to null out all of the object references in the list, in case this is the only reference to some object

clear: Code Logically, all we need to do is set this.numElems = 0 But it’s good practice to null out all of the object references in the list. Why? /** Empty this list */ public void clear( ) { for ( int k = 0; k < this.numElems; k++) { //optional this.elements[k] = null; // triggers a garbage collection if it is the only // reference } // DON’T DO: for (Object o : elements) { o = null; } WHY? this.numElems = 0; Additional invariant being maintained (and assumed by for loop above): elements[numElems..length-1] == null But it’s probably a good idea to null out all of the object references in the list, in case this is the only reference to some object

Method get /** Return object at position pos of this list The list is unchanged */ public E get( int pos) { return this.elements[pos]; } Anything wrong with this? Hint: what are the preconditions?

A Better get Implementation We want to catch out-of-bounds arguments, including ones that reference unused parts of array elements /** Return object at position pos of this list. 0 <= pos < size( ), or IndexOutOfBoundsException is thrown */ public E get( int pos) { if (pos < 0 || pos >= this.numElems) { throw new IndexOutOfBoundsException( ); } return (E) this.elements[pos]; Question: is a "throws" clause required? Exercise: write out the preconditions more fully Exercise: specify and implement the set method Exercise: rewrite the above with an assert statement

Method indexOf Sequential search for first “equal” object /** return first location of object o in this list if found, otherwise return –1 */ public int indexOf( Object o) { for ( int k = 0; k < this.size( ); k++) { E elem = this.get(k); if (elem.equals(o)) { // found item; return its position return k; } // item not found return -1; Exercise: write postconditions Could this be implemented in an abstract superclass?

Method contains /** return true if this list contains object o, otherwise false */ public boolean contains( Object o) { // just use indexOf return this.indexOf(o) != -1; } As usual, an alternate, implementation-dependent version is possible Exercise: define "this list contains object o" more rigorously

remove(pos): Specification /** Remove the object at position pos from this list. Return the removed element. 0 <= pos < size( ), or IndexOutOfBoundsException is thrown */ public E remove( int pos) { ... return removedElem; } Postconditions: quite a bit more complicated this time... Try writing them out! Key observation for implementation: we need to compact the array after removing something in the middle; slide all later elements left one position pos Definitely need to draw pictures here, to deduce what the internal data looks like, and how that appears to the external client What are some postconditions?

Array Before and After remove pos Before After – Wrong! After – Right! pos pos

remove(pos): Code /** Remove the object at position pos from this list. Return the removed element. 0 <= pos < size( ), or IndexOutOfBoundsException is thrown */ public E remove( int pos) { if (pos < 0 || pos >= this.numElems) { throw new IndexOutOfBoundsException( ); } E removedElem = this.elements[pos]; for (int k = pos+1; k < this.numElems; k++) { this.elements[k-1] = this.elements[k]; // slide k'th element left by one index this.elements[this.numElems-1] = null; // erase extra ref. to last element, for GC this.numElems--; return removedElem; Definitely need to draw pictures here, to deduce what the internal data looks like, and how that appears to the external client What are some postconditions?

remove(Object) /** Remove the first occurrence of object o from this list, if present. @return true if list altered, false if not */ public boolean remove(Object o) { int pos = indexOf(o); if (pos != -1) { remove(pos); return true; } else { return false; } Pre- and postconditions are not quite the same as remove(pos)

add Object at position Key implementation idea: /** Add object o at position pos in this list. List changes, so return true 0 <= pos < size( ), or IndexOutOfBoundsException is thrown */ public boolean add( int pos, E o) { … Key implementation idea: we need to make space in the middle; slide all later elements right one position Pre- and postconditions? pos

add(pos, o): Code /** Add object o at position pos in this list. List changes, so return true 0 <= pos < size( ), or IndexOutOfBoundsException is thrown */ public boolean add( int pos, E o) { if (pos < 0 || pos >= this.numElems) { throw new IndexOutOfBoundsException( ); } if (this.numElems >= this.elements.length) { // yuck; what can we do here? here's a temporary measure…. throw new RuntimeException("list capacity exceeded"); … continued on next slide …

add(pos, o) (continued) … //preconditions have been met // first create a space for ( int k = this.numElems - 1; k >= pos; k --) { // must count down! this.elements[k+1] = this.elements[k]; // slide k'th element right by one index } this.numElems ++; // now store object in the space opened up this.elements[pos] = o; // erase extra ref. to last element, for GC return true;

add Revisited – Dynamic Allocation Our original version of add checked for the case when adding an object to a list with no spare capacity But did not handle it gracefully: threw an exception Better handling: "grow" the array Problem: Java arrays are fixed size – can't grow or shrink Solution: Make a new array of needed size This is called dynamic allocation

Dynamic Allocation Algorithm allocate a new array with larger capacity, copy the elements from the old array to the new array, and replace the old array with the new one i.e., make the array name refer to the new array Issue: How big should the new array be?

Method add with Dynamic Allocation Following implementation has the dynamic allocation buried out of sight... /** Add object o to the end of this list @return true, since list is always changed by an add */ public boolean add( E o) { this.ensureExtraCapacity(1); this.elements[this.numElems] = o; this.numElems ++; return true; } /** Ensure that elements has at least extraCapacity free space, growing elements if needed */ private void ensureExtraCapacity( int extraCapacity) { … magic here …

ensureExtraCapacity /** Ensure that elements[] has at least extraCapacity free space, growing elements[] if needed */ private void ensureExtraCapacity( int extraCapacity) { if (this.numElems + extraCapacity > this.elements.length) { // we need to grow the array int newCapacity = this.elements.length * 2 + extraCapacity; E[] newElements = (E[]) new Object[newCapacity]; for ( int k = 0; k < this.numElems; k++) { newElements[k] = this.elements[k]; //copying old to new } this.elements = newElements; Note: this is ensure extra capacity, not add extra capacity (there is an if statement). Pre- and Post- conditions? Check the method System.arraycopy Draw pictures, before & after growing. Show external vs. internal view.

Method iterator Collection interface specifies a method iterator( ) that returns a suitable Iterator for objects of that class Key Iterator methods: boolean hasNext( ), E next( ) Method remove( ) is optional for Iterator in general, but expected to be implemented for lists. [left as an exercise] Idea: Iterator object holds... a reference to the list it is traversing and the current position in that list. Can be used for any List, not just ArrayList! Except for remove( ), iterator operations should never modify the underlying list

Method iterator In class MyArrayList /** Return a suitable iterator for this list */ public Iterator<E> iterator( ) { return new MyListIterator(this); }

Class MyListIterator (1) /** Iterator helper class for lists */ class MyListIterator<E> implements Iterator<E> { // instance variables private List<E> list; // the list we are traversing private int nextItemPos; // position of next element to visit (if any left) // invariant: 0 <= nextItemPos <= list.size( ) /** construct iterator object */ public SimpleListIterator(List<E> list) { this.list = list; this.nextItemPos = 0; } … Class isn't public, because it's not intended to be used by clients, just other classes in same package. Could introduce the notion of inner classes, to make this internalness clearer. I didn't, to avoid distracting from the ideas for how to implement an Iterator.

Class MyListIterator (2) /** return true if more objects remain in this iteration */ public boolean hasNext( ) { return this.nextItemPos < this.list.size( ); } /** return next item in this iteration and advance. Note: changes the state of the Iterator but not of the List @throws NoSuchElementException if iteration has no more elements */ public E next( ) { if ( ! hasNext( ) ) { throw new NoSuchElementException( ); E result = this.list.get(this.nextItemPos); this.nextItemPos ++; return result;

Design Question Why create a separate Iterator object? Couldn't the list itself have.. ...operations for iteration? hasNext( ) next( ) reset( ) //start iterating again from the beginning

Summary MyArrayList presents an illusion to its clients Appears to be a simple, unbounded list of elements Actually a more complicated array-based implementation Key implementation ideas: capacity vs. size/numElems sliding elements to implement (inserting) add and remove growing to increase capacity when needed growing is transparent to client Caution: Frequent sliding and growing is likely to be expensive….