Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Similar documents
Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

Array Based Lists. Collections

CSE 143 Au03 Midterm 2 Page 1 of 7

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

Class 26: Linked Lists

Outline. iterator review iterator implementation the Java foreach statement testing

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Exceptions and Design

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

ArrayList. Introduction. java.util.arraylist

The Java Collections Framework and Lists in Java Parts 1 & 2

CS 314 Final Fall 2011

Building Java Programs

CS 3 Introduction to Software Engineering. 5: Iterators

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Programming II (CS300)

List ADT. B/W Confirming Pages

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Lecture 9: Lists. MIT-AITI Kenya 2005

Programming II (CS300)

Queues. CITS2200 Data Structures and Algorithms. Topic 5

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

ITI Introduction to Computing II

Subclassing for ADTs Implementation

Arrays.

JAVA COLLECTION FRAMEWORK & SETS

Advanced Java Concepts Unit 2: Linked Lists.

Vector (Java 2 Platform SE 5.0) Overview Package Class Use Tree Deprecated Index Help

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

ITI Introduction to Computing II

CMSC 341 Lecture 7 Lists

List ADT. Announcements. The List interface. Implementing the List ADT

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CS Introduction to Data Structures Week 1 Thursday

Active Learning: Streams

ITI Introduction to Computing II

ITI Introduction to Computing II

EXAMINATIONS 2016 TRIMESTER 2

Introduction to Computing II (ITI 1121) Final Examination

CS350: Data Structures Stacks

Java Collection Framework

An Introduction to Data Structures

Arrays. Chapter Arrays What is an Array?

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

Need to access each element of a list. Use index to access an element of the list. It s quadratic, while:

Java Review: Objects

Building Java Programs

Algorithms. Produced by. Eamonn de Leastar

+ Abstract Data Types

Lists. The List ADT. Reading: Textbook Sections

CE204 Data Structures and Algorithms Part 2

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Collections and Iterators. Collections

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Lecture 6: ArrayList Implementation

CSC 321: Data Structures. Fall 2013

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Recursive Objects. Singly Linked List (Part 2)

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Linked Lists. Chapter 12.3 in Savitch

Practice exam for CMSC131-04, Fall 2017

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

Fundamentals of Object Oriented Programming

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CMSC 341. Linked Lists. Textbook Section 3.5

Introducing Design Patterns

Assertions, pre/postconditions

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

CS 307 Midterm 2 Fall 2009

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

6.005 Elements of Software Construction Fall 2008

ITI Introduction to Computing II

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Lecture 5: Implementing Lists, Version 1

CITS1001 week 4 Grouping objects lecture 2

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CSE 143. Lecture 7: Linked List Basics reading: 16.2

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Stacks and Queues. David Greenstein Monta Vista

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

Transcription:

Implementing a List in Java CSC 143 Java List Implementation Using Arrays Updated with Java 5.0 Generics Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List<E> concrete classes ArrayList<E>, LinkedList<E> same methods, different internals List<E> in turn extends (implements) Collection<E> Our current activities: Lectures on list implementations, in gruesome detail SimpleArrayList<E> is a class we develop as an example Projects in which lists are used 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-1 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-2 List Interface (review) int size( ) boolean isempty( ) boolean add( E obj ) boolean addall( Collection<? Extends E> other ) void clear( ) E get( int ) boolean set( int, E obj ) int indexof( Object obj ) boolean contains( Object obj ) E remove( int ) boolean remove( Object obj ) boolean add( int, E obj ) Iterator<E> iterator( ) Just an Illusion? Key concept: external view (the abstraction visible to clients) vs. internal view (the implementation) SimpleArrayList<E> may present an illusion to its clients Appears to be a simple, unbounded list of items Actually may be a complicated internal structure The programmer as illusionist... This is what abstraction is all about 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-3 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-4 Java Arrays (Review) Key difference from other languages: declaring an array doesn t create it it must be allocated with new int[ ] numbers; numbers = new int[42]; // creates numbers[0]..numbers[41] or int[ ] numbers = new int[42]; Size is fixed when array is allocated Element access: arrayname[ition] Every array object can report how many elements it contains int capacity = numbers.length 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-5 Using an Array to Implement a List Idea: store the list items in an array instance variable // Simple version of ArrayList for CSC 143 lecture example public class SimpleArrayList<E> implements List<E> { /** variable to hold all items of the list * (Cannot be E[ ] at this time maybe in the future.) private Object[ ] items; items Issues: How big to make the array? Why make the array of type Object[ ]? Algorithms for adding and deleting items (add and remove methods) Later: performance analysis of the algorithms 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-6 CSC 143 14-1

Space Management: Size vs. Capacity Idea: allocate extra space in the array, sibly more than is actually needed at a given time Definitions size: the number of items currently 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 items than the initial capacity? We ll get to that List Representation public class SimpleArrayList<E> implements List<E> { // instance variables private Object[ ] items; // items stored in items[ 0..numItems-1 ] private int numitems; // # of items currently in the list ( i.e. the 'size' ) // capacity?? Why no capacity variable?? size items // default capacity private static final int DEFAULT_CAPACITY = 5; Review: what is the static final? 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-7 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-8 Constructors We ll provide two constructors: /** Construct new list with specified capacity public SimpleArrayList( int capacity ) { size: /** Return size of this list public int size( ) { size, isempty: Code /** Construct new list with default capacity public SimpleArrayList( ) { this( DEFAULT_CAPACITY ); Review: this( ) means what? can be used where? why do we want it here? isempty: /** Return whether the list is empty (has no items) public boolean isempty( ) { return size( ) == 0; // OR return numitems == 0; Each choice has pros and cons: what are they? 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-9 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-10 Method add: simple version Assuming there is unused capacity /** Add object obj to the end of this list. @return true if the object was added successfully. This implementation always returns true. public boolean add( Object obj ) { Method add: simple version Assuming there is unused capacity /** Add object obj to the end of this list @return true, since list is always changed by an add public boolean add( E obj ) { if ( numitems < items.length ) {? obj addall(array or list) left as an exercise try it at home! else { // Already full what can we do here? here's a temporary measure. throw new RuntimeException( "list capacity exceeded ); 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-11 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-12 CSC 143 14-2

clear Logically, all we need to do is set numitems = 0 But it s good practice to null out all of the object references in the list. Why? /** Empty this list public void clear( ) { Method get /** Return object at ition of this list * The list is unchanged public E get( int ) { return (E)items[]; 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-13 Anything wrong with this? Hint: what are the preconditions? 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-14 A Better get Implementation We want to catch out-of-bounds arguments, including ones that reference unused parts of array items /** Return object at ition of this list. 0 <= < size( ), or IndexOutOfBoundsException is thrown public Object get( int ) { Method indexof Sequential search for first equal object /** return first location of object obj in this list if found, otherwise 1 public int indexof( Object obj ) { return items[]; Question: is a "throws" clause required? Exercise: write out the preconditions more fully Exercise: specify and implement the set method Exercise: write tconditions 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-15 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-16 Method contains /** return true if this list contains object obj, otherwise false public boolean contains( Object obj ) { Do we need a search loop here? Tradeoffs? Exercise: define "this list contains object obj" more rigorously remove(): Specification /** Remove object at ition from this list. Return removed item. 0 <= < size( ), or IndexOutOfBoundsException is thrown public E remove( int ) {... 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 items left one ition 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-17 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-18 CSC 143 14-3

Before Array Before and After remove remove() /** Remove object at ition from this list. Return removed item. 0 <= < size( ), or IndexOutOfBoundsException is thrown public E remove( int ) { After Wrong! After Right! 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-19 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-20 Interlude: Validate Position We ve written the code to validate the ition and throw an exception twice now suggests refactoring that code into a separate method /** Validate that a ition references an actual item in the list * @param Position to check * @throws IndexOutOfBoundsException if ition not valid, otherwise returns silently private void checkposition( int ) { remove(object) /** Remove the first occurrence of object obj from this list, if present. @return true if list altered, false if not public boolean remove( Object obj ) { 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-21 Pre- and tconditions are not quite the same as remove() 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-22 add Object at ition /** Add object obj at ition in this list. List changes, so return true 0 <= < size( ), or IndexOutOfBoundsException is thrown public boolean add( int, E obj ) { Key implementation idea: we need to make space in the middle; slide all later items right one ition Pre- and tconditions? add(, obj): Code /** Add object obj at ition in this list. List changes, so return true 0 <= < size( ), or IndexOutOfBoundsException is thrown public boolean add( int, E obj ) { checkposition( ); if ( numitems >= items.length ) { // out of room for now, throw new RuntimeException( "list capacity exceeded ); continued on next slide 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-23 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-24 CSC 143 14-4

add(, obj) (continued) // preconditions have been met // first create a space for ( int k = numitems - 1; k >= ; k-- ) { // must count down! items[k+1] = items[k]; // slide k'th item right by one index size ++; // now store object in the space opened up items[] = obj; // erase extra ref. to last item, for GC 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 & copy contents of old array to new, then add This is called dynamic allocation 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-25 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-26 Dynamic Allocation Algorithm Algorithm 1. allocate a new array with larger capacity, 2. copy the elements from the old array to the new array, and 3. 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 This implementation has the dynamic allocation hidden away... /** Add object obj to the end of this list @return true, since list is always changed by an add public boolean add( E obj ) { ensureextracapacity( 1 ); items[size] = obj; size ++; /** Ensure that items has at least extracapacity free space, growing items if needed private void ensureextracapacity( int extracapacity ) { magic here 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-27 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-28 How Much Extra Capacity? Observation: Growing the array is expensive New operation is relatively expensive Have to copy all the old entries If we increase the capacity by 1 or 2 at a time Every addition to the list is expensive Common heuristic: when the list fills up, double the capacity when adding a new item Makes average cost of adding a new item essentially the same as before More about this when we get to efficiency ensureextracapacity /** Ensure that items[ ] has at least extracapacity free space, growing items[ ] if needed private void ensurecapacity( int requestednumber ) { if ( size( ) + requestednumber <= items.length ) { return; int newcapacity = Math.max( size( ) + requestednumber, 2*items.length ); Object[ ] newlist = new Object[newCapacity]; for ( int i = 0; i < size( ); i++ ) { newlist[i] = items[i]; items = newlist; Pre- and Post- conditions? 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-29 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-30 CSC 143 14-5

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 ition 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 SimpleArrayList /** Return a suitable iterator for this list public Iterator<E> iterator( ) { return new SimpleListIterator( this ); 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-31 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-32 Class SimpleListIterator (1) /** Iterator helper class for lists class SimpleArrayListIterator<E> implements Iterator<E> { // instance variables private List<E> list; private int nextitem; // ition of next item in this iteration // not yet returned by next( ) /** * Initialize a new iterator for this list. public SimpleArrayListIterator( List<E> thelist ) { this.list = thelist; this.nextitem = 0; Class SimpleListIterator (2) /** Return true if there are more elements in the iteration * @return true if next can return an element public boolean hasnext( ) { return nextitem < list.size( ); /** Return the next item in the iteration * @return next item * @throws NoSuchElementException if there is no next element public E next( ) { if (!hasnext( ) ) { throw new NoSuchElementException( ); E result = list.get( nextitem ); nextitem++; return result; 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-33 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-34 Design Questions Why create a separate Iterator object? Couldn't the list itself have.....operations for iteration? hasnext( ) next( ) reset( ) //start iterating again from the beginning...private instance variable for nextpos? Would it have been better to implement the iterator as a nested class inside the simple list class? Yes, probably Summary SimpleArrayList presents an illusion to its clients Appears to be a simple, unbounded list of items Actually a more complicated array-based implementation Key implementation ideas: capacity vs. size sliding items 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. 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-35 5/11/2006 Original University of Washington; used by permission; modified by Vince Offenback 14-36 CSC 143 14-6