Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 #20 2011T1."— Presentation transcript:

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

2 © 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.

3 © 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

4 © 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 12345678910110 length: 12 flowers:

5 © 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.

6 © 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 0. 12345678910110 length: 12 123456781990 length: 200 000000000... null 0 flowers: marks:

7 © 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:

8 © Peter Andreae COMP102 20:8 123456780 45.580.0 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]; 199 200 length 0.0... 0.0 Index can be any int valued expression

9 © 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; }

10 © 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: 4 120 length: 3 45820 names: dimensions:

11 © 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; }

12 © 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

13 © 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

14 © 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; }

15 © 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- 12345671990 200 length null “C”“C” “ A+ ” ⋯ null grades.txt

16 © 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- 123451990 200 length CB+AA- ⋯ null A+B- grades: What's the problem? count: 6 grades.txt

17 © 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- 12345671990 200 length CB+ “C”“C” AA- null “ A+ ” ⋯ null A+B- null grades.txt 6543210

18 © 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 ]); }

19 © 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); }


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

Similar presentations


Ads by Google