120 likes | 226 Views
Introduction to Arrays. Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008. Reversing Input. public static void main( String args[] ) { Scanner in = new Scanner(System.in); System.out.println( “Numbers:" ); double num1; double num2; double num3;
E N D
Introduction to Arrays Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008
Reversing Input public static void main( String args[] ) { Scanner in = new Scanner(System.in); System.out.println( “Numbers:" ); double num1; double num2; double num3; num1 = in.nextDouble(); num2 = in.nextDouble(); num3 = in.nextDouble(); System.out.println( “Reverse:" ); System.out.println( num3 ); System.out.println( num2 ); System.out.println( num1 ); } • Problem: Read in three numbers and then print out the numbers in reverse order • Straightforward Java application • declare three variables of type double • read in the values using the Scanner object • print them out starting with the last variable read in Arrays
Generalizing a Program • Suppose we wanted the same program but wanted 10 instead of 3 numbers? • Suppose we wanted to read in 1000 numbers? • More than 3000 lines of code if we used the same approach! • Solution: arrays Arrays
double nums[]is also legal, but double[] numsis preferred, since it emphasizes that the type is double[] (“double array” or “array of doubles”) Arrays • Definition • collection of elements of the same type • each element is accessed through an index • In Java, • declaration: double[] nums; • creation: nums = new double[8]; • use: nums[3] = 6.6; • Note: starting index is 0 (0 to 7, above) Arrays
Visualizing an Array nums null Declare: double[] nums; Arrays
0.0 0 0.0 1 0.0 2 0.0 3 0.0 4 0.0 5 0.0 6 0.0 7 Visualizing an Array nums null Declare: double[] nums; Create: nums = new double[8]; Arrays
0.0 0 0.0 1 0.0 2 0.0 3 0.0 4 0.0 5 0.0 6 0.0 7 Visualizing an Array nums 6.6 Declare: double[] nums; Create: nums = new double[8]; Use: nums[3] = 6.6; Arrays
Reversing 10 numbers public static void main( String args[] ) { Scanner in = new Scanner(System.in); System.out.println( “Numbers:" ); double[] nums; nums = new double[3]; nums[0] = in.nextDouble(); nums[1] = in.nextDouble(); nums[2] = in.nextDouble(); System.out.println( “Reverse:" ); System.out.println( nums[2] ); System.out.println( nums[1] ); System.out.println( nums[0] ); } • Use arrays • declare double[] nums • create new double[10] • use indices 0, 1, 2 when referring to the different array elements • Statements still look redundant (how about using loops?) Arrays
Reversing 10 numbers (2) • Use arrays and loops • declare double[] nums • create new double[10] • use a for-statement to read in the numbers • use a for-statement to print them out in reverse public static void main( String args[] ) { Scanner in = new Scanner(System.in); System.out.println( “Numbers:" ); double[] nums; nums = new double[3]; for( int i = 0; i < 3; i++ ) nums[i] = in.nextDouble(); System.out.println( “Reverse:" ); for( int i = 2; i >= 0; i-- ) System.out.println( nums[i] ); } Arrays
Scaling up • What if you want to change the number of elements? • would have to find and change all places using 10 (including the 9 in the for loop) • very tedious and error-prone public static void main( String args[] ) { Scanner in = new Scanner(System.in); System.out.println( “Numbers:" ); double[] nums; nums = new double[10]; for( int i = 0; i < 10; i++ ) nums[i] = in.nextDouble(); System.out.println( “Reverse:" ); for( int i = 9; i >= 0; i-- ) System.out.println( nums[i] ); } Arrays
Reversing 10 numbers (3) • Use a constant to indicate the array size • Use that constant in the for loops • Just need to change one portion of the program when scaling up public static final int MAX = 3; public static void main( String args[] ) { Scanner in = new Scanner(System.in); System.out.println( “Numbers:" ); double[] nums; nums = new double[MAX]; for( int i = 0; i < MAX; i++ ) nums[i] = in.nextDouble(); System.out.println( “Reverse:" ); for( int i = MAX-1; i >= 0; i-- ) System.out.println( nums[i] ); } Arrays