240 likes | 312 Views
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[];
E N D
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[]; • Construct an array with the new keyword and the array length, or number of elements • ages = new int[100]; • int[] ages = new int[100];
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};
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
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
? ? ? ? ? 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
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]);
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]);
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]);
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]);
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]);
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
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
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
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
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
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
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
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
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’
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); }
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
In-class exercises Using arrays