120 likes | 136 Views
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University. Road Map. More array examples. Passing arrays to methods Reading: Chapter 5, Section 5.4 Reading: Chapter 6, Section 6.4 Reading: Chapter 7, Section 6.4. review.
E N D
Introduction to Computers and ProgrammingLecture 16: Arrays (cont)Professor: Evan KorthNew York University
Road Map • More array examples. • Passing arrays to methods • Reading: Chapter 5, Section 5.4 • Reading: Chapter 6, Section 6.4 • Reading: Chapter 7, Section 6.4
review • What is an array? • What is a subscript? • What is another term for a subscript? • Given an array of size n, what is the valid range of subscripts? • How can you obtain the size of an array in Java? • What happens if you try to access an element outside the valid range of subscripts? • What are the default values given to array elements in Java?
7.4 Examples Using Arrays (Cont.) • Histogram • Using the elements of an array as counters • Use a series of counter variables to summarize data
Declare frequency as array of 7ints Generate 6000 random integers in range 1-6 Increment frequency values at index associated with random number 1 // Fig. 7.7: RollDie.java 2 // Roll a six-sided die 6000 times. 3 import javax.swing.*; 4 5 public class RollDie { 6 7 public static void main( String args[] ) 8 { 9 int frequency[] = new int[ 7 ]; 10 11 // roll die 6000 times; use die value as frequency index 12 for ( int roll = 1; roll <= 6000; roll++ ) 13 ++frequency[ 1 + ( int ) ( Math.random() * 6 ) ]; 14 15 String output = "Face\tFrequency"; 16 17 // append frequencies to String output 18 for ( int face = 1; face < frequency.length; face++ ) 19 output += "\n" + face + "\t" + frequency[ face ]; 20 21 System.out.println(”Rolling a Die 6,00 times” ); 22 23 24 System.out.println ( output ); 25 26 27 System.exit( 0 ); 28 29 } // end main 30 31 } // end class RollDie RollDie.javaLine 9Declare frequency as array of 7ints Lines 12-13Generate 6000 random integers in range 1-6Line 13Increment frequency values at index associated with random number
7.4 Examples Using Arrays (Cont.) • Using arrays to analyze survey results • 40 students rate the quality of food • 1-10 Rating scale: 1 mean awful, 10 means excellent • Place 40 responses in array of integers • Summarize results
Declare responses as array to store 40 responses Declare frequency as array of 11int and ignore the first element For each response, increment frequency values at index associated with that response 1 // Fig. 7.8: StudentPoll.java 2 // Student poll program. 3 import javax.swing.*; 4 5 public class StudentPoll { 6 7 public static void main( String args[] ) 8 { 9 int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 11 4, 8, 6, 8, 10 }; 12 int frequency[] = new int[ 11 ]; 13 14 // for each answer, select responses element and use that value 15 // as frequency index to determine element to increment 16 for ( int answer = 0; answer < responses.length; answer++ ) 17 ++frequency[ responses[ answer ] ]; 18 19 String output = "Rating\tFrequency\n"; 20 21 // append frequencies to String output 22 for ( int rating = 1; rating < frequency.length; rating++ ) 23 output += rating + "\t" + frequency[ rating ] + "\n"; 24 JOpSystem.out.println( output ); 25 26 System.exit( 0 ); 27 28 } // end main 29 30 } // end class StudentPoll StudentPoll.javaLines 9-11Declare responses as array to store 40 responses Line 12Declare frequency as array of 11int and ignore the first elementLines 16-17For each response, increment frequency values at index associated with that response
7.4 Examples Using Arrays (Cont.) • Some additional points • When looping through an array • Index should never go below 0 • Index should be less than total number of array elements • When invalid array reference occurs • Java generates ArrayIndexOutOfBoundsException • Chapter 15 discusses exception handling
7.6 Passing Arrays to Methods • To pass array argument to a method • Specify array name without brackets • Array hourlyTemperatures is declared as int hourlyTemperatures [] = newint[24]; • The method call modifyArray( hourlyTemperatures ); • Passes array hourlyTemperatures to method modifyArray
Passing arrays to methods (cont) • In the method header, use similar syntax as that for array declaration: public static void modifyArray(int array [] ) or public static void modifyArray(int [] array )
7.5 References and Reference Parameters • Two ways to pass arguments to methods • Pass-by-value • Copy of argument’s value is passed to called method • In Java, every primitive is pass-by-value • Pass-by-reference • Caller gives called method direct access to caller’s data • Called method can manipulate this data • Improved performance over pass-by-value • In Java, every object is simulated pass-by-reference • In Java, arrays are objects • Therefore, arrays are passed to methods by reference • Technically, we are using pass by value but the “value” we are passing is a reference to the array.