1 / 24

Class 19

Class 19. Class 19 Objectives. Use arrays for more efficient coding. Arrays. An array stores multiple data items of the same type in a single storage location Declare an array with the element type, a set of square brackets, and the array identifier name int[] ages; or int ages[];

Download Presentation

Class 19

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

  2. Class 19 Objectives • Use arrays for more efficient coding

  3. Arrays • An array stores multiple data items of the same type in a single storage location • Declare an array with the element type, a set of square brackets, and the array identifier name • int[] ages; or int ages[]; • Construct an array with the new keyword and the array length, or number of elements • ages = new int[100]; • int[] ages = new int[100];

  4. Arrays • Retrieve the array’s length using the length property • int size = arrayName.length • Assign values and access array elements using the index number of the element • An index number is assigned to each element, beginning at zero and progressing by integers • temperature[3] = 78; • Declare, construct, and assign values with one statement • boolean[] results = {true, false, true, false, true};

  5. Arrays • Index numbers can be any expression that evaluates to an integer • Array elements can be used anywhere a variable is used • overtime = (hours[6] - 40) * rate[6] * 1.5

  6. One-Dimensional Arrays int n[ ] = {32, 27, 64, 18, 95}; 32 27 64 18 95 n[0] n[1] n[2] n[3] n[4] n.length would return 5

  7. ? ? ? ? ? 5 5 5 ? ? ? ? ? ? ? ? ? 3 ? 3 int z[ ] = new int[5]; z[0] = 5; z[4] = 3; z[5] = 10; 10 Out of bounds/syntax error

  8. One-Dimensional Arrays int x[ ] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); or int x[ ] = {9,11,13}; for (int i = 0; i<3; i = i+1) System.out.println(x[i]);

  9. One-Dimensional Arrays int x[ ] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); or int x[ ] = {9,11,13}; for (int i = 0; i<3; i++) // shortcut for i = i+1 System.out.println(x[i]);

  10. One-Dimensional Arrays int x[ ] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 or int x[ ] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]);

  11. One-Dimensional Arrays int x[ ] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 11 or int x[ ] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]);

  12. One-Dimensional Arrays int x[ ] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 11 13 or int x[ ] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]);

  13. One-Dimensional Arrays int x[ ] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); or int x[ ] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); Value of i

  14. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 0 Value of i

  15. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 0 Value of i

  16. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 0 1 Value of i

  17. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 11 or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 1 Value of i

  18. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 11 or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 2 1 Value of i

  19. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); 9 11 13 or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 2 Value of i

  20. One-Dimensional Arrays int x[3] = {9,11,13}; System.out.println(x[0]); System.out.println(x[1]); System.out.println(x[2]); ; 9 11 13 or int x[3] = {9,11,13}; for (int i = 0; i<3; i++) System.out.println(x[i]); 2 3 Value of i

  21. Write a for loop to initialize all the elements in a 20 integer array ‘values’ to zero. Begin for loop int values[ ]=new int[20]; Initialize counter for ( ) int i=0; i<20; i++ Set limit for counter Increment counter values[i]=0; Initialize element in array ‘values’

  22. Write a program that asks the user for the number of hours worked by 6 employees. Save these hours in an array. int hours []= new int[6]; for ( i=i+1) int i=0; i<6; {answer = JOptionPane.showInputDialog( “Enter hours worked”); hours[i]= Integer.parseInt(answer); }

  23. Write a program that initializes an array to the days of each month and displays those days. int Days[]={31,28,31,30, 31,30,31,31, 30,31,30,31); Initialize array Begin for loop Initialize counter for (int i=0; i < 12; i++) { System.out.println(“Month” + (i + 1) + “has” + Days[i] + “days”;} Set limit for counter Increment counter Display each next element of array

  24. In-class exercises Using arrays

More Related