Array Based Lists. Collections

Similar documents
Building Java Programs

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

CSE 143 Lecture 4. Preconditions

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

Building Java Programs

CSE 143 Lecture 2. reading:

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

CSE 143. Lecture 7: Linked List Basics reading: 16.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

Programming II (CS300)

Programming Abstractions

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

Adam Blank Lecture 4 Winter 2015 CSE 143. Computer Programming II

CS 200 Objects and ArrayList Jim Williams, PhD

CSE 143 Lecture 4. ArrayList. Reading: slides created by Marty Stepp

CSE 143. Computer Programming II

CSE 143X. Accelerated Computer Programming I/II

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

Using arrays to store data

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

CS 106A, Lecture 19 ArrayLists

Lecture 6: ArrayList Implementation

Unit testing. JUnit. JUnit and Eclipse. A JUnit test class 3/6/17. A method is flagged as a JUnit test case.

Building Java Programs

Unit testing. unit testing: Looking for errors in a subsystem in isolation. The basic idea: JUnit provides "assert" commands to help us write tests.

Programming II (CS300)

CS 101 Spring 2006 Final Exam Name: ID:

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

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

Computer Science 210 Data Structures Siena College Fall 2018

Introduction to Object-Oriented Programming

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

CS Programming I: ArrayList

CITS1001 week 4 Grouping objects

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

CSE 143 Lecture 20. Circle

Building Java Programs

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

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

Linked Lists. Chapter 12.3 in Savitch

Assertions, pre/postconditions

ECE 122. Engineering Problem Solving with Java

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

CS Introduction to Data Structures Week 1 Thursday

Building Java Programs

Slides are adapted from the originals available at

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

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Topic 3 Encapsulation - Implementing Classes

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

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

AP Computer Science. ArrayLists

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

Binghamton University. CS-140 Fall Chapter 7.7. Lists. Java Library Data Structures

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

Interfaces, collections and comparisons

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

STUDENT LESSON A15 ArrayList

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

List ADT. B/W Confirming Pages

A Linked Structure. Chapter 17

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

Big O & ArrayList Fall 2018 Margaret Reid-Miller

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic

Linked Lists. References and objects

CSE 331. Generics (Parametric Polymorphism)

1. ArrayList and Iterator in Java

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Linked List. ape hen dog cat fox. tail. head. count 5

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4

Chapter 4 Defining Classes I

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

Exceptions and Design

Programming Languages and Techniques (CIS120)

Building Java Programs

Intro to Computer Science II

CMSC 202. Containers

Arrays.

AP CS Unit 7: Interfaces. Programs

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

CS 307 Midterm 2 Spring 2008

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Computer Sciences 302 Exam 2 Information & Sample Exam

CSC 1052 Algorithms & Data Structures II: Lists

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Advanced Java Concepts Unit 2: Linked Lists.

CS 106A, Lecture 20 ArrayLists and HashMaps

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Classes, interfaces, & documentation. Review of basic building blocks

CSCI 1103: Array-based Data Structures

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

Transcription:

Array Based Lists Reading: RS Chapter 15 1 Collections Data structures stores elements in a manner that makes it easy for a client to work with the elements Specific collections are specialized for particular types of operations Ordering Duplicates Add/remove Common set of operations: add, remove, get, contains, size 2 1

From Reges & Stepp Lecture Notes 3 Lists List: an ordered collection of elements Accessible by a zero-based index Size: number of elements in the list Elements may be added to an empty list, at the front, middle, and back From Reges & Stepp Lecture Notes 4 2

Scenario Suppose we had a file of integers Representing grades, counts, etc. We want to calculate metrics and manipulate the list of ints Average, min, max, quartiles, print them in reverse, remove even numbers, etc. We would use an ArrayList to solve Automatically grows Simplifies working with an array But how does it really work? How is the ArrayList class implemented? 5 Working with ArrayList Constructing ArrayList<Integer> list = new ArrayList<Integer>(); Adding an element to the end of the list list.add(42); Adding an element at an index list.add(3, 42); //(index, element) Removing an element list.remove(3); //(index) Getting an element from an index list.get(3); //(index) An ArrayList is working with an array. But how? 6 3

Working with an Array Constructing int [] list = new int[?]; //how many elements? Adding an element to the end of the list list[?] = 42; //what index is the end of the list? Adding an element at an index list[3] = 42; //what happens to the rest of the list? Removing an element int v = list[3]; //rest of the list? Getting an element from an index int v = list[3]; 7 Partially Filled Arrays ArrayLists use the concept of partially filled arrays to simplify implementation of common client functionality int [] list = new int[10]; //capacity is 10 int size = 0; //size is the number of spots used list[size++] = 5; list[size++] = 7; size list[size++] = 14; list[size++] = 4; list[size++] = ; 42 35 1 5 7 14 4 0 1 2 3 4 5 6 7 8 9 8 4

ArrayList (of ints) Abstracts the details about how to interact with the array As a partially filled array Encapsulates the array and size Provides operations to manipulate the list public class ArrayIntList { private int [] list; private int size; public ArrayIntList() { public boolean add(int element) { public void add(int idx, int element) { public int remove(int idx) { public int get(int idx) { public int size() { 9 ArrayIntList Design ArrayIntList is a library class Encapsulates helpful functionality Client uses the functionality to accomplish meaningful tasks Tests (to evaluate library works) Client programs 10 5

Testing Lists Test each List method Empty list Front of the list Middle of the list Back of the list After each operation Length is correct Contents are correct (are the elements in the right order?) Then test the methods in combination maybe add usually works, but fails after you call remove what happens if I call add then size? remove then tostring? make multiple calls; maybe size fails the second time only 11 Testing ArrayIntList() Start by testing the constructor Should create an empty list of integers public class ArrayIntListTest { @Test public void testarrayintlist() { ArrayIntList l = new ArrayIntList(); assertequals(0, l.size()); 12 6

Testing ArrayIntList.add(element) Test adding elements to the end of the list Check size AND that elements are in the right order after the add list.add(); size 45 5 7 14 4 0 1 2 3 4 5 6 7 8 9 Implementation to Pass Test Implement add() size is the index where the new last element will go Don t forget to increment size! Implement size() & get() Straightforward methods & useful for testing other functionality Main coverage through tests for other functionality We will test get() a bit more thoroughly later 14 7

Testing ArrayIntList.add(idx, e) How do we add to the front and middle of the list? list.add(0, 5); size 45 57 14 4 0 1 2 3 4 5 6 7 8 9 15 Testing ArrayIntList.add(idx, e) How do we add to the front and middle of the list? list.add(0, 5); list.add(2, 11); size 56 5 7 14 11 4 0 1 2 3 4 5 6 7 8 9 16 8

Implementation to Pass Test Implement add(idx, element) Right shift order of the elements you shift matters! 17 Size vs. Capacity What happens when a client tries to add an element at capacity+1? list.add(21); size 10 5 7 14 11 11 4 6 31 15 2 0 1 2 3 4 5 6 7 8 9 18 9

Reaching Capacity What should happen when we reach capacity? Depends on the requirements Maybe there is a hard cap Throw an exception Maybe the array should automatically grow Resize the array Create a new array of larger size typically double Copy values from old array into new array Update field list to refer to the new array 19 Update to add() Methods add() methods should check capacity and increase if needed public boolean add(int element) { ensurecapacity(size + 1); public void add(int idx, int element) { ensurecapacity(size + 1); 20 10

Testing remove(idx) How do we remove an element from the list? int v = list.remove(3); size 65 rtn 11 5 7 14 11 11 4 0 1 2 3 4 5 6 7 8 9 21 Size vs. Capacity What happens if a client tried to access an element in a partially-filled array that is past the size but within the capacity (bounds) of the array? list.get(8); size 6 5 7 14 11 11 4 0 1 2 3 4 5 6 7 8 9 22 11

Preconditions & Postconditions Precondition: something that your method assumes is true before starting execution In documentation Enforce? Postcondition: something that you promise will be true after execution of your method Assuming the precondition is met Test to make sure this is true! 23 Bad Precondition Test What is wrong with handling a violation of a precondition in the following manner? //Returns the element at the given index public int get(int idx) { if (idx < 0 idx >= size) { System.out.println( Bad index! + idx); return -1; 24 12

Enforcing the Precondition Throw an exception to the client! //Returns the element at the given index public int get(int idx) { if (idx < 0 idx >= size) { throw new IndexOutOfBoundsException(); 25 Convenience Methods Useful methods for the client indexof() returns the first index of the element if found and -1 otherwise isempty() returns true if size is 0 contains() returns true if the element is in the list Why are they useful to the client? if (list.size() == 0) { if (list.isempty()) { 26

Implementing your own Lists We ve reached the point in the semester where using the Java Collections Framework may not be allowed Depends on the project Project 1 OK Project 2 OK Project 3 NO Labs NO You ll start implementing your own custom lists in the labs! 27 14