Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

Similar documents
Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

1.4 Arrays. A Foundation for Programming. Arrays. Many Variables of the Same Type. This lecture. Store and manipulate huge quantities of data.

! 52 playing cards in a deck. ! 5 thousand undergrads at Princeton. ! 1 million characters in a book. ! 10 million audio samples in an MP3 file.

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write

Debugging [continued]

Debugging [continued]

CS 112 Introduction to Programming

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. Many Variables of the Same Type. Many Variables of the Same Type

COMPUTER SCIENCE. 4. Arrays. Section 1.4.

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Module 7: Arrays (Single Dimensional)

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

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

Inf1-OOP. Encapsulation and Collections. Perdita Stevens, adapting earlier version by Ewan Klein. March 2, School of Informatics

Inf1-OOP. Encapsulation. Object Oriented Programming. Encapsulation Accessing Data Immutability. Encapsulation and Collections.

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections

Classes Classes 2 / 36

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

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

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

New Concepts. Lab 7 Using Arrays, an Introduction

Classes Classes 2 / 35

Arrays. Eng. Mohammed Abdualal

Pace University. Fundamental Concepts of CS121 1

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Lab Assignment Three

... Lecture 12. Pointers

CS111: PROGRAMMING LANGUAGE II

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Problems with simple variables

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Introduction to Computing II (ITI 1121) Final Examination

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Object Oriented Programming. Java-Lecture 6 - Arrays

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

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

Computer Science is...

Exercise 4: Loops, Arrays and Files

Fall 2017 CISC124 9/16/2017

Scope of this lecture

3. Java - Language Constructs I

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Instructor: Eng.Omar Al-Nahal

Relationship between Pointers and Arrays

Inf1-OP. Functions aka Static Methods. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. School of Informatics

Arrays. Arrays. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Example. Password generator

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Computer Science & Engineering 150A Problem Solving Using Computers

Tutorial 4. Values and References. Add One A. Add One B. Add One C

CS 231 Data Structures and Algorithms, Fall 2016

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

CS1150 Principles of Computer Science Arrays

COS 126 General Computer Science Spring Written Exam 1

Full file at

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Introduction to Object-Oriented Programming

( &% class MyClass { }

1 Short Answer (5 Points Each)

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Getting started with Java

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays to the rescue!

Array. Array Declaration:

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

Computer Science II Data Structures

Chapter 9 Introduction to Arrays. Fundamentals of Java

Last Class. More on loops break continue A bit on arrays

Class Foo. instance variables. instance methods. Class FooTester. main {

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Object Oriented Programming 2013/14. Final Exam June 20, 2014

Arithmetic Compound Assignment Operators

Functions / Static Methods. Why are functions so helpful? Inf1-OP. Find Nearest Neighbour to a Central Point

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Chapter 6 - Pointers

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

Praktische Softwaretechnologie

1007 Imperative Programming Part II

7. Arrays, More Java Looping

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Transcription:

Inf1-OP Arrays 1 Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein Arrays School of Informatics February 26, 2018 1 Thanks to Sedgewick&Wayne for much of this content Arrays Arrays: allow us to store and manipulate large quantities of data. An array is an indexed sequence of values of the same type. Examples 52 playing cards in a deck. 17,000 undergraduates in UoE. 1 million characters in a book. 10 million audio samples in an MP3 file. 4 billion nucleotides in a DNA strand. 90 billion Google queries per year. 50 trillion cells in the human body. index value 0 Rebecca 1 Isla 2 Brooke 3 Megan 4 Niamh 5 Eilidh 6 Eva 7 Abbie 8 Skye 9 Aimee (From 100 most popular Scottish girls names, 2007) Many Variables of the Same Type How do we initialize 10 variables of the same type? double a0, a1, a2, a3, a4, a5, a6, a7, a8, a9; a0 = 0.0; a1 = 0.0; a2 = 0.0; a3 = 0.0; a4 = 0.0; a5 = 0.0; a6 = 0.0; a7 = 0.0; a8 = 0.0; a9 = 0.0; a4 = 3.0; a4 = 8.5; double x = a4 + a5;

Many Variables of the Same Type Many Variables of the Same Type How do we initialize 10 variables of the same type? How do we initialize 1 million variables of the same type? // easy alternative double[] a = new double[10]; a[4] = 3.0; a[8] = 8.0; double x = a[4] + a[8]; declares, creates and initializes // just as easy with large arrays double[] a = new double[1000000]; a[123456] = 3.0; a[987654] = 8.0; double x = a[123456] + a[987654]; Arrays in Java, 1 First index Element (at index 3) Java has special support for arrays: To make an array: declare, create and initialize it. 01 1 2 3 4 indices Declare an array int[] arrayofints; Array length is 5 Create an array of length 10 arrayofints = new int[10];

Arrays in Java, 2 Java has special support for arrays: To make an array: declare, create and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. int n = 10; // size of array double[] a; // declare the array a = new double[n]; // create the array a[i] = 0.0; // initialise each elt Compact alternative: Declare, create and initialize in one statement. Default Initialization of Arrays Each array element is automatically initialized to a default value: int: 0 double: 0.0 boolean: false String: null int n = 10; // size of array double[] a = new double[n]; // declare, create, init Types of Array All elements of a given array must be of the same type. Array Types int[] double[] String[] char[] Array of Strings: Alternative Initialization Syntax for Arrays Shorthand syntax for initializing arrays. Handy if you only have a few data items. String[] names = {"Rebecca", "Isla", "Brooke", "Megan", "Niamh"; int[] mynums = { 0, 7, 9, 1, 4 ; double[] morenums = { 2.5, -0.1, 33.0 ; String[] names = new String[5]; names[0] = "Rebecca"; names[1] = "Isla"; names[2] = "Brooke"; names[3] = "Megan"; names[4] = "Niamh";

The Length of Arrays Arrays: Another Example Given an array a, check the length of the array: a.length first element is a[0] second element is a[1]... last element is a[a.length-1] If an array index is too small or too large, Java throws run-time error: ArrayIndexOutOfBoundsException public class ArrayEx { public static void main(string[] args) { String[] names = { "Rebecca", "Isla", "Brooke", "Megan", "Niamh" ; System.out.println(names.length); System.out.println(names[1]); System.out.println(names[names.length]); Output 5 Isla Exception in thread "main" java.lang.arrayindexoutofboundsexception: To get at last element, use names[names.length-1]. Vector Dot Product Dot Product: Given two vectors x[] and y[] of length n, their dot product is the sum of the products of their corresponding components. States double[] x = { 0.3, 0.6, 0.1 ; double[] y = { 0.5, 0.1, 0.4 ; double sum = 0.0; for (int i = 0; i < x.length; i++) { sum = sum + x[i] * y[i]; i x[i] y[i] x[i]*y[i] sum ------------- 0 0.30 0.50 0.15 0.15 1 0.60 0.10 0.06 0.21 2 0.10 0.40 0.04 0.25 Array-processing Examples, 1 Create an array with random values double[] a = new double[n]; a[i] = Math.random(); Print the array values, one per line System.out.println(a[i]); Find the maximum of the array values double max = a[0]; for (int i = 1; i < n; i++) { if (a[i] > max) max = a[i];

Array-processing Examples, 2 Array-processing Examples, 3 Compute the average of the values in an array (length n) of doubles. double sum = 0.0; sum += a[i]; double average = sum / n; Copy one array (called a, of doubles, length n) to another. double[] b = new double[n]; b[i] = a[i]; Array-processing Examples, 4 Reverse the elements within an array (called a, of doubles, length n). Using a copy double[] b = new double[n] b[i] = a[n-1-i]; In Place for (int i = 0; i < n/2; i++) { double temp = a[i]; a[i] = a[n-1-i]; a[n-1-i] = temp; Setting Array Values at Run Time Print a random card. Output String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ; String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" ; int i = (int) (Math.random() * 13); // between 0 and 12 int j = (int) (Math.random() * 4); // between 0 and 3 System.out.println(rank[i] + " of " + suit[j]); 7 of Spades Jack of Diamonds

Setting Array Values at Run Time Shuffling typical array-processing String[] deck = new String[52]; code changes values at for (int i = 0; i < 13; i++) { runtime for (int j = 0; j < 4; j++) { deck[4 * i + j] = rank[i] + " of " + suit[j]; for (int k = 0; k < deck.length; k++) { System.out.println(deck[k]); Q: In what order does the program print the deck? Output 1 Output 2 Given an array, rearrange its elements in random order. Shuffling algorithm: 1. In iteration i, pick random card from deck[i] through deck[n-1], with each card equally likely. 2. Exchange it with deck[i]. 2 of Clubs 2 of Diamonds 2 of Hearts 2 of Spades 3 of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs Shuffling a Deck of Cards: Putting Everything Together Remark on capitalisation public class Deck { public static void main(string[] args) { String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" ; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ; int SUITS = suit.length; int RANKS = rank.length; int N = SUITS * RANKS; String[] deck = new String[N]; avoid "hardwired" constants for (int i = 0; i < RANKS; i++) build the deck for (int j = 0; j < SUITS; j++) deck[suits * i + j] = rank[i] + " of " + suit[j]; for (int i = 0; i < N; i++) { int randcard = i + (int) (Math.random() * (N - i)); String temp = deck[randcard]; deck[randcard] = deck[i]; deck[i] = temp; shuffle In Java, roughly, everything except class names (and a few related things) starts with a lower case letter as a matter of convention. But SUITS etc. were written in capitals. Why? Because if we were teaching the whole language these would have been constant fields not only constant, but enforced as constant by the compiler. But we aren t teaching the features that let you do that in this course, so always start your variable and method names with lower case. for (int i = 0; i < N; i++) System.out.println(deck[i]); print shuffled deck

Shuffling a Deck of Cards Output Output Two-Dimensional Arrays % java Deck Jack of Clubs 4 of Spades 5 of Clubs 10 of Diamonds 2 of Hearts Queen of Clubs 8 of Hearts 5 of Hearts 3 of Clubs 7 of Hearts 10 of Hearts 6 of Hearts Jack of Spades 3 of Hearts % java Deck 4 of Spades 2 of Diamonds 5 of Hearts 7 of Diamonds 3 of Hearts 10 of Hearts 2 of Clubs King of Diamonds Queen of Diamonds 10 of Clubs 3 of Spades 7 of Hearts 8 of Clubs 3 of Clubs Examples of two-dimensional arrays: Table of data for each experiment and outcome. Table of grades for each student and assignment. Table of grayscale values for each pixel in a 2D image. Mathematical abstraction: matrix Java abstraction: 2D Array Two-Dimensional Arrays in Java Array access: Use a[i][j] to access element in row i and column j. Zero-based indexing: Row and column indices start at 0. Setting 2D Array Values at Compile Time Initialize 2D array of doubles by listing values. Each element of the array p is itself an array of type double[]. int m = 10; int n = 3; double[][] a = new double[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { a[i][j] = 0.0; Initialize a 10-by-3 array of doubles a[ ][ ] a[5] a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2] a[3][0] a[3][1] a[3][2] a[4][0] a[4][1] a[4][2] a[5][0] a[5][1] a[5][2] a[6][0] a[6][1] a[6][2] a[7][0] a[7][1] a[7][2] a[8][0] a[8][1] a[8][2] a[9][0] a[9][1] a[9][2] A 10-by-3 array double[][] p = { {.02,.92,.02,.02,.02, {.02,.02,.32,.32,.32, {.02,.02,.02,.92,.02, {.92,.02,.02,.02,.02, {.47,.02,.47,.02,.02, ; 0.02 0.92 0.02 0.02 0.02 0.02 0.02 0.32 0.32 0.32 0.02 0.02 0.02 0.92 0.02 0.92 0.02 0.02 0.02 0.02 0.47 0.02 0.47 0.02 0.02 p[1][3] 0.02 0.92 0.02 0.02 0.02 0.02 0.02 0.32 0.32 0.32 0.02 0.02 0.02 0.92 0.02 0.92 0.02 0.02 0.02 0.02 0.47 0.02 0.47 0.02 0.02 ro

Matrix Addition Matrix Addition: given two n-by-n matrices a and b, define c to be the n-by-n matrix where c[i][j] is the sum a[i][j] + b[i][j]. a[ ][ ].70.20.10.30.60.10.50.10.40 a[1][2] Matrix Multiplication Matrix Multiplication: given two n-by-n matrices a and b, define c to be the n-by-n matrix where c[i][j] is the dot product of the i th row of a[][] and the j th column of b[][]. a[ ][ ].70.20.10.30.60.10.50.10.40 row 1 double[][] c = new double[n][n]; for (int j = 0; j < n; j++) { c[i][j] = a[i][j] + b[i][j]; b[ ][ ] c[ ][ ].80.30.50.10.40.10.10.30.40 1.5.50.60.40 1.0.20.60.40.80 b[1][2] c[1][2] double[][] c = new double[n][n]; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { c[i][j] += a[i][k] * b[k][j]; b[ ][ ] c[ ][ ] column 2.80.30.50.10.40.10.10.30.40.59.32.41.31.36.25.45.31.42 c[1][2] =.30.50 +.60.10 +.10.40 =.25 Enhanced for loop, 1 Ordinary for loops are easy to get wrong! Often there s a better way: int[] numbers = {2, 5, 6, 1, 0, 5; Ordinary for loop for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); Enhanced for loop for (int num : numbers) System.out.println(num); Enhanced for loop, 2 Also called for-each loop, with : pronounced in. On each iteration, an element of the iterable gets assigned to the loop variable. Loop gets executed once for each element in the iterable. Easier and more concise: no need to initialise loop counter, increment, set termination condition but less flexible; no access to the loop counter. Use them whenever you don t need access to the loop counter. Typical use: when you need access to all the elements of an array, but you don t care about their indexes. General form: for ( variable declaration : iterable ){ NB the variable must have same type as elements in iterable.

Enhanced for loop, 3 Summary Another Example: Right String[] words = {"hello", "world", "yes", "we", "can"; for (String w : words) { System.out.println(w); Another Example: Wrong String[] words = {"hello", "world", "yes", "we", "can" ; for ( int w : words) { System.out.println(w); Arrays: Method of storing large amounts of data. Almost as easy to use as primitive types. We can directly access an element given its index. Enhanced for loop: Good alternative to ordinary for loop where you just want to iterate over an array, and don t care about the indexes. Learning Outcomes for this week Declare and initialize an array of elements of a given type. Use zero-based indexing to access and assign values to array elements. Use shorthand notation for creating and initializing short arrays. Check the length of an array. Use for-loops to iterate over the elements of an array, using the length of the array to limit iteration; also print out the values of the array; assign random values to the array; compute the minimum, average and maximum of an array; copy one array to another; reverse the elements of an array. Create 2-dimensional arrays and access their elements. Use for-each loops to iterate when an explicit loop counter is not required. This Week s Reading Java Tutorial pp51-57 i.e. now it s time to read carefully the section on Arrays within Chapter 3, Language Basics, that I suggested skimming over before.