Chapter 9 Arrays 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 9-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 1 Chapter 9 Arrays
Chapter g objectives After you have read and studied this chapter, you should be able to e Manipulate a collection of data values using an array. e Declare and use an array of primitive data types in writing a program e Declare and use an array of objects in writing a program Describe how a two-dimensional array is implemented as an array of arrays e Manipulate a collection of objects using a vector. e Use a MultiInput Box object from the javabook package to input an array of strings Define a method that accepts an array as its parameter and a method that returns an array e Describe how the self-reference pointer works and use it in methods C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 2 Chapter 9 Objectives After you have read and studied this chapter, you should be able to Manipulate a collection of data values using an array. Declare and use an array of primitive data types in writing a program. Declare and use an array of objects in writing a program. Describe how a two-dimensional array is implemented as an array of arrays. Manipulate a collection of objects using a vector. Use a MultiInputBox object from the javabook package to input an array of strings. Define a method that accepts an array as its parameter and a method that returns an array. Describe how the self-reference pointer works and use it in methods
Array Basics r Suppose you need to handle up to 300 Student objects in a program for maintaining a high school alumni list, would you use 300 variables pose, you need to process daily temperatures for a month period in a science project, Would you use 65 variables rYou can, but would you? r An array is a collection of data values of the same data type If your program s to deal with 100 integers, 500 Account objects, real numbers, etc, you will use an array. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 3 Array Basics Suppose you need to handle up to 300 Student objects in a program for maintaining a high school alumni list, would you use 300 variables? Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can, but would you? An array is a collection of data values of the same data type. If your program needs to deal with 100 integers, 500 Account objects, 365 real numbers, etc., you will use an array
Arrays of Primitive Data Types r Array Declaration [] //variation 1 [ j //variation 2 r Array creation new r Example Variation 1 Variation 2 double[] rainfall double rainfall[] rainfall rainfall new double[12]i new double[12] An array is like an ob ject C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 4 Arrays of Primitive Data Types Array Declaration [ ] //variation 1 [ ] //variation 2 Array Creation = new [ ] Example double[ ] rainfall; rainfall = new double[12]; Variation 1 double rainfall [ ]; rainfall = new double[12]; Variation 2 An array is like an object!
Accessing Individual Elements r Individual elements in an array accessed with the indexed expression double[ rainfall= new double[12] rainfall 01234567891011 This indexed expression The index of the first rainfall[2] refers to the element at position in an array is o position#2 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 5 Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[] rainfall = new double[12]; rainfall 0 1 2 3 4 5 6 7 8 9 10 11 rainfall[2] This indexed expression refers to the element at position #2 The index of the first position in an array is 0
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