1 / 11

Introduction to Arrays

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;

kara
Télécharger la présentation

Introduction to Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Arrays Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008

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

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

  4. 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

  5. Visualizing an Array nums null Declare: double[] nums; Arrays

  6. 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

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

  8. 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

  9. 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

  10. 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

  11. 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

More Related