《JAVA OOP开发》英文版 Chapter 9 objectives

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第五章 循环结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第二章 数据类型与运算符.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第二讲 数据类型与算术运算.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第九讲 函数.doc
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第四章 MCS-51单片机存储器的扩展.ppt
- 《网络安全设计》 第一章 安全设计简介.ppt
- 《网络安全设计》 第二章 创建网络安全计划.ppt
- 《网络安全设计》 第三章 确定网络安全威胁.ppt