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

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 Various Methods of Populating Arrays Randomly generated integers.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
©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.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review 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 More 2D arrays COMP 102 #27.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
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.
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 Classes, Objects, Fields,
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
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.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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 2D arrays COMP 102 # T1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
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 ArrayLists: varying size arrays.
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.
Computer Programming for Engineers
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 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.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
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];
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 Arrays COMP 102 # T1.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Chapter 7 Part 1 Edited by JJ Shepherd
Writing to a File Open a File Wrap it in a new PrintStream object.
Building Java Programs
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Presentation transcript:

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

© Peter Andreae COMP102 20:2 Outline ArraysTextbook: Ch 7 for : shorthand version of while Administrivia Test on Thursday: A – Ley: MC LT103 (usual lecture room) Li – Ross: CO LT122(in Cotton, opposite security office) Rundle – Z: KK LT301(in New Kirk, by the bridge) New version of comp102 library: Please download and install, if you work at home 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 20: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 20: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 referred to 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 20:5 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 Doesn't put values in the array.

© Peter Andreae COMP102 20:6 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 20:7 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, this does not copy the array! (just the reference/ID of the object) length: numbers:

© Peter Andreae COMP102 20: 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.5; marks[n] = 80.0; UI.printf(“Student #%d got %4.1f marks”, 20, marks[20] ); marks[5] = ( marks[4] + marks[6] ) / 2; marks[n] = marks[n-1]; length Index can be any int valued expression

© Peter Andreae COMP102 20:9 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; }

© Peter Andreae COMP102 20:10 Initialising the contents of an array Can specify the intial values (and size) of the array by listing them (in {…} ): String [ ] names = new String [ ] { “pondy”, “rashina”, “sharon”, “marcus” }; int [ ] dimensions = new int [ ] { 20, 45, 8 }; 1230 “ rashina ”“ sharon ”“ marcus ”“ pondy ” length: length: names: dimensions:

© Peter Andreae COMP102 20:11 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 num = 0; while (num < marks.length) { marks[num] = essay[num]*0.6 + exam[num]*0.4; num = num+1; }

© Peter Andreae COMP102 20:12 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 20:13 For loop For loop puts the intialisation condition increment together, at the front of the loop But the meaning is exactly the same as the while loop for(statement) ;;expressionstatement InitialisationConditionIncrement

© Peter Andreae COMP102 20:14 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 i = 0 ; i < marks.length ; i++) { marks[ i ] = essay[ i ]*0.6 + exam[ i ]*0.4; }

© Peter Andreae COMP102 20:15 don ’ t go past end of array don ’ t go past end of file Reading data from a file into an array Suppose grades.txt contains student grades String [ ] grades = new String [200]; try { Scanner sc = new Scanner (new File(“grades.txt”)); int count = 0; while ( sc.hasNext() && count < grades.length) { grades[count] = sc.next(); count++; } } catch (IOException e) { UI.printf( “File failure %s\n”, e); } A+ C B+ B- A A length null “C”“C” “ A+ ” ⋯ null grades.txt

© Peter Andreae COMP102 20:16 Printing out the number of A's String [ ] grades = new String [200]; 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); A+ C B+ B- A A length CB+AA- ⋯ null A+B- grades: What's the problem? count: 6 grades.txt

© Peter Andreae COMP102 20:17 Reading data from a file into an array Standard pattern: array with an associated count String [ ] grades = new String [200]; int count = 0; try { Scanner sc = new Scanner (new File(“grades.txt”)); while ( sc.hasNext() && count < grades.length) { grades[count] = sc.next(); count++; } catch (IOException e) { UI.printf( “File failure %s\n”, e); } A+ C B+ B- A A length CB+ “C”“C” AA- null “ A+ ” ⋯ null A+B- null grades.txt

© Peter Andreae COMP102 20:18 Exercise Write code to fill an array of numbers then print them out: The array should hold doubles. The numbers should be the square roots of the integers from 1 to 100. public void squareRootTable(){ double [ ] table = new double [110]; for ( int i = 1; i <= 100; i++){ table[ i ] = Math.sqrt(i); } for ( int i = 1; i <= 100; i++){ System.out.printf(“Sqrt of %d is %6.3f \n”, i, table[ i ]); }

© Peter Andreae COMP102 20:19 Exercise Write a method that will read a sequence of stock prices from a file into an array and return the array. public double [ ] readPrices(String fname){ double [ ] prices = new double [200]; try { Scanner sc = new Scanner (new File(fname)); int count = 0; while ( sc.hasNext() && count < 200) { prices [ count] = sc.nextDouble(); } sc.close(); return prices; } } catch (IOException e) { System.out.printf( “File failure %s\n”, e); }