Array. 9 January 2015 OSU CSE 1

Similar documents
More About Heaps. 14 June 2017 OSU CSE 1

Java Interfaces. 6 April 2016 OSU CSE 1

Repeated Arguments. 26 April 2013 OSU CSE 1

Set4. 8 February 2019 OSU CSE 1

CIS 341 Final Examination 4 May 2017

Class object initialization block destructor Class object

Kernel Implementations I. 27 June 2013 OSU CSE 1

Introduction to Programming Using Java (98-388)

Kernel Implementations III. 6 May 2013 OSU CSE 1

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Cantor s Diagonal Argument for Different Levels of Infinity

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Exam 1 - (20 points)

Lecture 8: Iterators and More Mutation

Lecture 5 Sorting Arrays

What is the Java Collections Framework?

Lecture Notes on Memory Layout

Module 11. Collections and Iterators. Adapted from Absolute Java, Rose Williams, Binghamton University

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

Binary Search. Roland Backhouse February 5th, 2001

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Expressions and Casting

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

COS 126 General Computer Science Spring Written Exam 1

CIS 341 Final Examination 3 May 2011

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Type Checking. Chapter 6, Section 6.3, 6.5

Java Loose Ends. 11 December 2017 OSU CSE 1

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

CMSC 132: Object-Oriented Programming II

Java Collections Framework. 24 April 2013 OSU CSE 1

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

CS558 Programming Languages


Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

2. [20] Suppose we start declaring a Rectangle class as follows:

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Collections, Maps and Generics

Chapter 4 Defining Classes I

Procedural programming. [verb-oriented] Tell the computer to do this. (Spring 2012)

EECS 2011 M: Fundamentals of Data Structures

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE331 Winter 2014, Midterm Examination February 12, 2014

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CS162: Introduction to Computer Science II

Protection Levels and Constructors The 'const' Keyword

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Announcements/Follow-ups

THE CATHOLIC UNIVERSITY OF EASTERN AFRICA A. M. E. C. E. A

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Vector and Free Store (Vectors and Arrays)

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

Lecture Topics. Administrivia

QUIZ. What is wrong with this code that uses default arguments?

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

1 10 3:30 5: :30 1:30 206, ICICS/CS

CSE 143 SAMPLE MIDTERM

CS558 Programming Languages

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

COEN244: Class & function templates

Templates Templates are functions or classes that are parameterized. We have already seen a few in STL:

CS61B Lecture #2. Public Service Announcements:

Syntax of Eiffel: a Brief Overview

CSE 230 Computer Science II (Data Structure) Introduction

EXAM Computer Science 1 Part 1

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS S-08 Arrays and Midterm Review 1

Object Orientated Analysis and Design. Benjamin Kenwright

1007 Imperative Programming Part II

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

The compilation process is driven by the syntactic structure of the program as discovered by the parser

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Material from Recitation 1

Short Notes of CS201

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

IMACS: AP Computer Science A

Java Comparable interface

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 Notes on Contracts

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS201 - Introduction to Programming Glossary By

Exercise 8 Parametric polymorphism November 18, 2016

Default arguments, documentation

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Fundamentals of Programming Session 15

Transcription:

Array 9 January 2015 OSU CSE 1

Array The Array component family allows you to manipulate arrays in a way that overcomes surprising limitations of built-in Java arrays, but retains the time/space performance of built-in arrays Another generic type like Sequence and Set A best practice alternative from the OSU CSE components to the built-in Java array (more like built-in arrays than Sequence is) 9 January 2015 OSU CSE 2

Problems With Built-in Arrays Suppose you want to declare an array of type T, where T is a generic parameter, e.g.: T[] table = new T[100]; Oops; this won t compile! The reason is complicated, having to do with backward compatibility when Java generics were introduced and the inability to call a constructor for type T, among other oddities 9 January 2015 OSU CSE 3

Problems With Built-in Arrays Or, suppose you want to declare an array of type Queue<T>, e.g.: Queue<T>[] table = new Queue<T>[100]; Oops; this won t compile either! Yet this is exactly the kind of thing you want to do when creating a hashtable: the buckets are collections of a parameterized type 9 January 2015 OSU CSE 4

Workarounds There are various workarounds that can be used, but they are ugly and generate warnings that you should generally heed rather than ignore These problems do not arise with the Array component family, so it can (and should) be used in many cases where you might be inclined to try built-in Java arrays 9 January 2015 OSU CSE 5

The Intuition The rather simple idea is to provide a generic interface Array<T> that provides the same functionality as built-in arrays, but where the entries may be of any type T The data representation used in Array1L<T> is a built-in array But this data representation is hidden from the client, along with one of the workarounds mentioned earlier 9 January 2015 OSU CSE 6

Interfaces and Classes Standard extends extends Iterable ArrayKernel extends Array implements Array1L 9 January 2015 OSU CSE 7

Interfaces and Classes Standard extends extends Iterable ArrayKernel ArrayKernel has contracts for four methods: setentry entry maybeexamined length extends Array implements Array1L 9 January 2015 OSU CSE 8

Interfaces and Classes Standard Array has contracts for Iterable two methods: extends replaceentry extends exchangeentries ArrayKernel Array Array1L extends implements 9 January 2015 OSU CSE 9

Interfaces and Classes There is really Standard abstract Iterable class as usual in the chain here, but it is not extends shown extends because these slides describe the client view, ArrayKernel and a client needs only interface Array and class Array1L. extends Array Array1L implements 9 January 2015 OSU CSE 10

Mathematical Model ARRAY_MODEL is ( entries: string of T, examinableindices: finite set of integer) exemplar a constraint for all i: integer where (i is in a.examinableindices) (0 <= i and i < a.entries ) type ArrayKernel is modeled by ARRAY_MODEL 9 January 2015 OSU CSE 11

Mathematical Model ARRAY_MODEL is ( entries: string of T, examinableindices: finite set of integer) exemplar a constraint for all i: integer The set examinableindices where (i is keeps in a.examinableindices) track of the indices in the Array that have been initialized (0 <= i and by i a < call a.entries ) to setentry, because only these indices may be type ArrayKernel is examined modeled by a by call ARRAY_MODEL to entry. 9 January 2015 OSU CSE 12

Constructor from int There is no no-argument constructor, but there is a constructor with one parameter int n Requires: n >= 0 Ensures: this.entries = n and this.examinableindices = {} 9 January 2015 OSU CSE 13

Example Code State Array<Integer> ai = new Array1L<>(3); 9 January 2015 OSU CSE 14

Example Code State Array<Integer> ai = new Array1L<>(3); ai = (<?,?,?>, {}) 9 January 2015 OSU CSE 15

Example All we know from the constructor Code contract is that the length of this string of integer is 3; we do not know its actual value. Array<Integer> ai = new Array1L<>(3); State ai = (<?,?,?>, {}) 9 January 2015 OSU CSE 16

setentry void setentry(int i, T x) Sets the entry at index i of this to x. Aliases: reference x Updates: this Requires: 0 <= i and i < this.entries Ensures: this.entries = #this.entries[0, i) * <x> * #this.entries[i+1, #this.entries ) and this.examinableindices = #this.examinableindices union {i} 9 January 2015 OSU CSE 17

Example Code State ai = (<?,?,?>, {}) z = 70 ai.setentry(1, z); 9 January 2015 OSU CSE 18

Example Code State ai = (<?,?,?>, {}) z = 70 ai.setentry(1, z); ai = (<?, 70,?>, {1}) z = 70 9 January 2015 OSU CSE 19

Example Note the alias created here, which Code you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it. State ai = (<?,?,?>, {}) z = 70 ai.setentry(1, z); ai = (<?, 70,?>, {1}) z = 70 9 January 2015 OSU CSE 20

Another Example Code State ai = (<?, -8,?>, {1}) z = 70 ai.setentry(1, z); 9 January 2015 OSU CSE 21

Another Example Code State ai = (<?, -8,?>, {1}) z = 70 ai.setentry(1, z); ai = (<?, 70,?>, {1}) z = 70 9 January 2015 OSU CSE 22

entry T entry (int i) Reports the entry at index i of this. Aliases: reference returned by entry Requires: i is in this.examinableindices Ensures: <entry> = this.entries[i, i+1) 9 January 2015 OSU CSE 23

entry T entry(int i) i is within bounds, i.e.: Reports the entry at position 0 <= i iof this. and i < this.entries Aliases: reference returned by entryat Note that this implies the index Requires: i is in this.examinableindices Ensures: <entry> = this.entries[i, i+1) 9 January 2015 OSU CSE 24

Example Code State ai = (<?, 70, -3>, {1, 2}) z = 584 z = ai.entry(1); 9 January 2015 OSU CSE 25

Example Code State ai = (<?, 70, -3>, {1, 2}) z = 584 z = ai.entry(1); ai = (<?, 70, -3>, {1, 2}) z = 70 9 January 2015 OSU CSE 26

Example Note the alias Code created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it. z = ai.entry(1); State ai = (<?, 70, -3>, {1, 2}) z = 584 ai = (<?, 70, -3>, {1, 2}) z = 70 9 January 2015 OSU CSE 27

maybeexamined boolean maybeexamined(int i) Reports whether the entry at index i of this may be examined using entry. Ensures: maybeexamined = i is in this.examinableindices 9 January 2015 OSU CSE 28

Example Code State ai = (<?, 70, -3>, {1, 2}) boolean ok = ai.maybeexamined(1); 9 January 2015 OSU CSE 29

Example Code State ai = (<?, 70, -3>, {1, 2}) boolean ok = ai.maybeexamined(1); ai = (<?, 70, -3>, {1, 2}) ok = true 9 January 2015 OSU CSE 30

length int length() Reports the length of this. Ensures: length = this.entries 9 January 2015 OSU CSE 31

replaceentry T replaceentry(int i, T x) Replaces the entry at index i of this with x, and returns the old entry at that index. Aliases: reference x Updates: this.entries Requires: i is in this.examinableindices Ensures: this.entries = #this.entries[0, i) * <x> * #this.entries[i+1, #this ) and <replaceentry> = #this.entries[i, i+1) 9 January 2015 OSU CSE 32

Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 w = 94 w = ai.replaceentry(2, z); 9 January 2015 OSU CSE 33

Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 w = 94 w = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = 58 w = -3 9 January 2015 OSU CSE 34

Example Note the alias Code created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it. State ai = (<?, 70, -3>, {1, 2}) z = 58 w = 94 w = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = 58 w = -3 9 January 2015 OSU CSE 35

Another Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 z = ai.replaceentry(2, z); 9 January 2015 OSU CSE 36

Another Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 z = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = -3 9 January 2015 OSU CSE 37

Another Example This use of Code the method avoids creating an alias: it swaps z with the entry previously at index 2. State ai = (<?, 70, -3>, {1, 2}) z = 58 z = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = -3 9 January 2015 OSU CSE 38

exchangeentries void exchangeentries(int i, int j) Exchanges entries at indices i and j of this. Updates: this.entries Requires: {i, j} is subset of this.examinableindices Ensures: this.entries = [#this.entries with entries at indices i and j exchanged] 9 January 2015 OSU CSE 39

Example Code State ai = (<?, 70, -3>, {1, 2}) ai.exchangeentries(1, 2); 9 January 2015 OSU CSE 40

Example Code State ai = (<?, 70, -3>, {1, 2}) ai.exchangeentries(1, 2); ai = (<?, -3, 70>, {1, 2}) 9 January 2015 OSU CSE 41

iterator Iterator<T> iterator() Returns an iterator over a set of elements of type T. Ensures: ~this.seen * ~this.unseen = [string of entries of this.entries, in order by increasing index, where index is in this.examinableindices] 9 January 2015 OSU CSE 42

Resources OSU CSE Components API: Array http://cse.osu.edu/software/common/doc/ 9 January 2015 OSU CSE 43