Array processing -1 double[ rainfall new double [12]i The public constant length returns the double annualAverage, capacity of an array sum for (int i=0; i< rainfall. length; i++)t rainfall[i] inputBox. getDouble(" Rainfall for month +(i+1)) sum + rainfall[ili annualAverage sum rainfall length C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 6 Array Processing – 1 double[] rainfall = new double[12]; double annualAverage, sum = 0.0; for (int i = 0; i < rainfall.length; i++) { rainfall[i] = inputBox.getDouble("Rainfall for month " + (i+1) ); sum += rainfall[i]; } annualAverage = sum / rainfall.length; The public constant length returns the capacity of an array
Array processing-2 double[ rainfall new double [12]i String[] monthName new String [12] onthName [0] JAnuary i monthName[1]=February The same pattern for the remaining ten months double annualAverage, sum =0.0 for (int rainfall length i++)t rainfall[i] inputBox getDouble(" Rainfall for month monthName [i] sum + rainfall[ili The actual month annualAverage sum rainfall length name instead of a numbe C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 7 Array Processing – 2 double[] rainfall = new double[12]; String[] monthName = new String[12]; monthName[0] = “January”; monthName[1] = “February”; … double annualAverage, sum = 0.0; for (int i = 0; i < rainfall.length; i++) { rainfall[i] = inputBox.getDouble("Rainfall for month " + monthName[i] ); sum += rainfall[i]; } annualAverage = sum / rainfall.length; The same pattern for the remaining ten months. The actual month name instead of a number
Array processing-3 r Compute the average rainfall for each quarter. //assume rainfall is declared and initialized properly double[] quarterAverage new double[4]i for (int i =0; i< 4 1++) surm 0; for (int j=0:j<3:j++) //compute the sum of sum + rainfall[3*i jI //one quarter quarterAverage[i] = sum /3.0 //Quarter (i+l) average C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 8 Array Processing – 3 Compute the average rainfall for each quarter. //assume rainfall is declared and initialized properly double[] quarterAverage = new double[4]; for (int i = 0; i < 4; i++) { sum = 0; for (int j = 0; j < 3; j++) { //compute the sum of sum += rainfall[3*i + j]; //one quarter } quarterAverage[i] = sum / 3.0; //Quarter (i+1) average }
Array Initialization r Like other data types, it is possible to declare and initialize an array at the same time. int[] number = 2,4,6,8 1 doub1e[] sampling Data={2.443,8.99,12.3,45.009,18.2, 9.00,3.123,22.084,18.08}; String[] monthName =("January,February ,"March 'April n,"May ll June I Jul August","September",October November December)i r The capacity of the array is set to the number of elements in the list number length samplingData length 9 monthName length 12 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 9 Array Initialization Like other data types, it is possible to declare and initialize an array at the same time. int[] number = { 2, 4, 6, 8 }; double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2, 9.00, 3.123, 22.084, 18.08 }; String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; The capacity of the array is set to the number of elements in the list. number.length samplingData.length monthName.length 4 9 12
Arrays of objects r An array of primitive data is a powerful tool, but even more powerful is an array of objects By combining the power of arrays and objects, we can structure programs in a clean and logical organization r If we have only arrays of primitives then to represent a collection of Person or Account objects for example, we need to use several different arrays, one for names, one for addresses, and so forth t is very cumbersome and error-prone r An array of person objects or an array of Account objects will result in a more concise and easier-to understand code C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 10 Arrays of Objects An array of primitive data is a powerful tool, but even more powerful is an array of objects. By combining the power of arrays and objects, we can structure programs in a clean and logical organization. If we have only arrays of primitives, then to represent a collection of Person or Account objects, for example, we need to use several different arrays, one for names, one for addresses, and so forth. This is very cumbersome and error-prone. An array of Person objects or an array of Account objects will result in a more concise and easier-tounderstand code