Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 #21 2014T1.

Slides:



Advertisements
Similar presentations
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102.
Advertisements

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Java Unit 9: Arrays Declaring and Processing Arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 7 Part 1 Edited by JJ Shepherd
Review for Final Exam.
Topics discussed in this section:
Review for Final Exam.
Building Java Programs
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1

© Peter Andreae COMP102 21:2 Outline ArraysTextbook: Ch 7 for : shorthand version of while Administration Terms Test #2: Next Monday, 5-6pm (6-7 for people doing PHYS114) Rooms:HM LT205, KK LT303, MC LT103 Topics: Loops, Files, Creating, defining and using Objects, Event Driven Input (GUIs), Note: Chapter 6 of textbook is on GUI's. It uses a more complex design than this course (it uses inheritance and private inner classes). Read it only if you find it helpful.

© Peter Andreae COMP102 21:3 Storing lots of values In most programs you need to remember lots of values: all the flowers on the screen, so that you can make them all grow, bloom, etc all the numbers from a file, so that you can sort them them all all the names and marks of students in a class, so you can update the marks and display them Can’t use lots of variables: Flower f1, f2, f3, f4, f5, f6, f7, f8, f9, … f100; int n1, n2, n3, n4, n5, … n1000; Could use a file, but then can only read/write one at a time Could sometimes use a String, but that’s (usually) messy

© Peter Andreae COMP102 21:4 An array is an object with a sequence of places Fixed length = number of places (recorded in the array object) All elements are of the same type Each element specified by its index (an int expression) flowers[ 4 ] ← name of the element of shapes with index 4 flowers[ n-3 ] Note: Counting from 0 ! Array knows its length: flowers.length Arrays length: 12 flowers:

© Peter Andreae COMP102 21:5 Garden Program: lots of flowers flowers: length: 12

© Peter Andreae COMP102 21:6 Declaring and Creating Arrays Declare a variable to hold an array object by putting [ ] after the type of the elements: Flower[ ] flowers; String[ ] keywords; private double[ ] marks; Create an array object with new and the length of the array: new Flower[12]; new String[50]; new double[200]; As usual, can combine declaration and initialisation: String [ ] keywords = new String [50]; What does the new array contain? Creates a place that can hold an array Doesn ’ t create the array itself Creates an array object Doesn ’ t declare a variable to hold it

© Peter Andreae COMP102 21:7 Initial values in a new array Arrays of objects initialised with null (the “no object here” value) Arrays of numbers initialised to length: length: null 0 flowers: marks:

© Peter Andreae COMP102 21:8 Garden Program public class Garden implements UIButtonListener{ private Flower[ ] = new Flower[12]; length: 12 flowers: null

© Peter Andreae COMP102 21:9 Using an array Can act on the whole array to pass to a method to assign to another variable : this.processFlowers(flowers); int maxNum = this.findMax(numbers); int [ ] windowSizes = numbers Note, passing as argument and assignment do not copy the array! (just the reference/ID of the object) length: numbers:

© Peter Andreae COMP102 21: Using an Array You can refer to an individual place in the array to put a value in that place to access the value in that place double [ ] marks = new double [200]; int n=4; : marks[5] = 45.6; marks[6] = ( marks[5] + marks[7] ) / 2; marks[n-1] = 80.0; marks[n] = marks[n-1]; length Index can be any int valued expression

© Peter Andreae COMP102 21:11 Garden Program public class Garden implements UIButtonListener{ private Flower[ ] flowers = new Flower[12]; public Garden(){ int i = 0; while (i < this.flowers.length) { this.flowers[ i ] = new Flower(70+i*50, 400); i = i + 1; } this.redraw(); } public void redraw(){ int i = 0; while (i < this.flowers.length) { this.flowers[ i ].draw(); i = i + 1; } length: 12 flowers:

© Peter Andreae COMP102 21:12 Using an Array You must not use an index that is “out of bounds” marks[-3] or marks[500] will cause an error. index must be in range 0 … length-1 If not certain, can check first: // swap values at positions n and 0 if ( 0<=n && n < marks.length ){ marks[n] = marks[0]; marks[0] = marks[n]; } double temp = marks[n]; marks[n] = marks[0]; marks[0] = temp; } length n: 7 temp:

© Peter Andreae COMP102 21:13 Initialising the contents of an array Can specify the initial values (and size) of an array by listing the values in {..,..,..} : String [ ] names = new String [ ] { “Pondy”, “John”, “Sharon”, “Marcus” }; int [ ] dimensions = new int [ ] { 20, 45, 8 }; 1230 “ John ”“ Sharon ”“ Marcus ”“ Pondy ” length: length: names: dimensions:

© Peter Andreae COMP102 21:14 Using an Array: loops Usually, want to act on lots of elements ⇒ access and assign array elements inside a loop: Print out all the marks: int num = 0; while (num < marks.length) { UI.printf( “Student %3d got %4.1f”, num, marks[ num ]); num = num +1; } Compute final marks from essay marks and exam marks: int stNum = 0; while (stNum < marks.length) { marks[stNum] = essay[stNum]*0.6 + exam[stNum]*0.4; stNum++; }

© Peter Andreae COMP102 21:15 Using an Array : for loops Standard pattern for processing each element of an array: int i = 0 ; while (i < data.length ){ … data[ i ] … ; i = i+1 ; } The for loop is a shorthand: for (int i = 0 ; i < data.length ; i = i+1 ) { … data[ i ] … ; } or for (int i = 0 ; i < data.length ; i++ ) { … data[ i ] … ; } Shorthand: same effect as i = i + 1; but the value is the value of i before incrementing it

© Peter Andreae COMP102 21:16 For loop For loop puts the intialisation  once, before the loop body is run at all condition  tested each time, before loop body run increment  run each time, after loop body run together, at the front of the loop But the meaning is (almost) exactly the same as the while loop (scope of loop variable is different) for(statement) ;;expressionstatement InitialisationConditionIncrement

© Peter Andreae COMP102 21:17 Using an Array: using for loops Same as before, but with for loop: ⇒ easier to read Print out all the marks: for (int num = 0 ; num < marks.length ; num++) { UI.printf( “Student %3d got %4.1f”, num, marks[ num ]); } Compute final marks from essay marks and exam marks: for (int stNum = 0 ; stNum < marks.length ; stNum++) { marks[stNum] = essay[stNum]*0.6 + exam[stNum]*0.4; }

© Peter Andreae COMP102 21:18 Garden Program public class Garden implements UIButtonListener{ private Flower[ ] flowers = new Flower[12]; public Garden(){ for (int i = 0; i < this.flowers.length; i++) { this.flowers[ i ] = new Flower(70+i*50, 400); } this.redraw(); } public void redraw(){ for(int i = 0; i < this.flowers.length; i++) { this.flowers[ i ].draw(); } public void bloomAll(){ for(int i = 0; i < this.flowers.length; i++) { this.flowers[ i ].bloom(); } length: 12 flowers:

© Peter Andreae COMP102 21:19 Reading data from a file into an array Suppose garden.txt contains locations of 12 flowers try { Scanner sc = new Scanner (new File(“garden.txt”)); this.flowers = new Flower[12]; for (int i = 0; i<12; i++) { this.flowers[ i ] = new Flower(sc.nextInt(), sc.nextInt()); count++; } sc.close(); } catch (IOException e) { UI.printf( “File failure %s\n”, e); } : length: 12 null “C”“C” “ A+ ” ⋯ null garden.txt

© Peter Andreae COMP102 21:20 Reading data from a file into an array Suppose grades.txt contains some student grades with the number of grades first String [ ] grades; try { Scanner sc = new Scanner (new File(“grades.txt”)); grades = new String [ sc.nextInt() ]; int count = 0; while (count < grades.length && sc.hasNext() ) { grades[count] = sc.next(); count++; } sc.close(); } catch (IOException e) { UI.printf( “File failure %s\n”, e); } 86 A+ C B+ B- A A- : length: 86 null “C”“C” “ A+ ” ⋯ null grades.txt number of grades

© Peter Andreae COMP102 21:21 Printing out the number of A's String [ ] grades; try { … …  read grades from the file  … /n", e); } int numAs = 0; for (int i = 0; i<grades.length; i++ ) { if ( grades[ i ].startsWith("A") { numAs++; } UIprintf( "%d A's out of %d grades\n", numAs, grades.length); 86 A+ C B+ B- A A length: 86 CB+AA- ⋯ A A+B- ⋯ grades: grades.txt