Download presentation
Presentation is loading. Please wait.
Published byCharla Russell Modified over 8 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 #21 2014T1
2
© 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.
3
© 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
4
© 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 12345678910110 length: 12 flowers:
5
© Peter Andreae COMP102 21:5 Garden Program: lots of flowers flowers: 12345678910110 length: 12
6
© 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
7
© 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 0. 12345678910110 length: 12 123456781990 length: 200 000000000... null 0 flowers: marks:
8
© Peter Andreae COMP102 21:8 Garden Program public class Garden implements UIButtonListener{ private Flower[ ] = new Flower[12]; 12345678910110 length: 12 flowers: null
9
© 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:
10
© Peter Andreae COMP102 21:10 123456780 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]; 199 200 length 0.0 80.0 Index can be any int valued expression 80.045.60.0... 0.0 22.80.0
11
© 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; } 12345678910110 length: 12 flowers:
12
© 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; } 123456780 45.580.0 199 200 length 42.0... 27.013.08.511.715.0 27.017.0 n: 7 temp:
13
© 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: 4 120 length: 3 45820 names: dimensions:
14
© 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++; }
15
© 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
16
© 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
17
© 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; }
18
© 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(); } 12345678910110 length: 12 flowers:
19
© 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); } 48 96 150 300 400 130 380 172 89 364 158 230 : 480 310 1234567850 length: 12 null “C”“C” “ A+ ” ⋯ null garden.txt
20
© 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- : 1234567850 length: 86 null “C”“C” “ A+ ” ⋯ null grades.txt number of grades
21
© 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- 12345850 length: 86 CB+AA- ⋯ A A+B- ⋯ grades: grades.txt
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.