 2006 Pearson Education, Inc. All rights reserved. 1 8 8 Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 Pearson Education, Inc. All rights reserved Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
1 Console methods Write and WriteLine also have the capability to display formatted data. Figure 3.17 shows another way to use the WriteLine method. Outline.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2009 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2009 Pearson Education, Inc. All rights reserved Arrays.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Chapter 4 Introduction to Classes, Objects, Methods and strings
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 Pearson Education, Inc. All rights reserved Passing Arrays to Methods To pass array argument to a method – Specify array name without.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© 2016 Pearson Education, Ltd. All rights reserved.
8 Arrays.
Introduction to Classes and Objects
Java How to Program, Late Objects Version, 10/e
7 Arrays.
8 Arrays.
Introduction to Classes and Objects
7 Arrays.
MSIS 655 Advanced Business Applications Programming
6 C Arrays.
7 Arrays.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Arrays

 2006 Pearson Education, Inc. All rights reserved. 2 Now go, write it before them in a table, and note it in a book. — Isaiah 30:8 To go beyond is as wrong as to fall short. — Confucius Begin at the beginning, … and go on till you come to the end: then stop. — Lewis Carroll

 2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  What arrays are.  To use arrays to store data in and retrieve data from lists and tables of values.  To declare arrays, initialize arrays and refer to individual elements of arrays.  To use the foreach statement to iterate through arrays.  To pass arrays to methods.  To declare and manipulate multidimensional arrays.  To write methods that use variable-length argument lists.  To read command-line arguments into an application.

 2006 Pearson Education, Inc. All rights reserved Introduction 8.2 Arrays 8.3 Declaring and Creating Arrays 8.4 Examples Using Arrays 8.5 Case Study: Card Shuffling and Dealing Simulation 8.6 foreach Statement 8.7 Passing Arrays and Array Elements to Methods 8.8 Passing Arrays by Value and by Reference 8.9 Case Study: Class GradeBook Using an Array to Store Grades 8.10 Multidimensional Arrays 8.11 Case Study: Class GradeBook Using a Rectangular Array 8.12 Variable-Length Argument Lists 8.13 Using Command-Line Arguments 8.14 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System 8.15 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved. 5 Fig. 8.1 | A 12-element array.

 2006 Pearson Education, Inc. All rights reserved. 6 Common Programming Error 8.1 In an array declaration, specifying the number of elements in the square brackets of the declaration (e.g., int[ 12 ] c; ) is a syntax error.

 2006 Pearson Education, Inc. All rights reserved. 7 Good Programming Practice 8.1 For readability, declare only one variable per declaration. Keep each declaration on a separate line and include a comment describing the variable being declared.

 2006 Pearson Education, Inc. All rights reserved. 8 Outline InitArray.cs

 2006 Pearson Education, Inc. All rights reserved. 9 Outline InitArray.cs

 2006 Pearson Education, Inc. All rights reserved. 10 Good Programming Practice 8.2 Constants also are called named constants. Such variables often make applications more readable than applications that use literal values (e.g., 10 )—a named constant such as ARRAY_LENGTH clearly indicates its purpose, whereas a literal value could have different meanings based on the context in which it is used. Another advantage to using named constants is that if the value of the constant must be changed, it is necessary to change it only in the declaration, thus reducing the cost of maintaining the code.

 2006 Pearson Education, Inc. All rights reserved. 11 Outline InitArray.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 12 Outline InitArray.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 8.2 Assigning a value to a named constant after it has been initialized is a compilation error.

 2006 Pearson Education, Inc. All rights reserved. 14 Common Programming Error 8.3 Attempting to declare a named constant without initializing it is a compilation error.

 2006 Pearson Education, Inc. All rights reserved. 15 Outline SumArray.cs

 2006 Pearson Education, Inc. All rights reserved. 16 Outline BarChart.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 17 Outline BarChart.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 18 Outline RollDie.cs

 2006 Pearson Education, Inc. All rights reserved. 19 Outline StudentPoll.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 20 Outline StudentPoll.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 21 Error-Prevention Tip 8.1 An exception indicates that an error has occurred in an application. You often can write code to recover from an exception and continue application execution, rather than abnormally terminating the application. Exception handling is discussed in Chapter 12.

 2006 Pearson Education, Inc. All rights reserved. 22 Error-Prevention Tip 8.2 When writing code to loop through an array, ensure that the array index remains greater than or equal to 0 and less than the length of the array. The loop-continuation condition should prevent the accessing of elements outside this range.

 2006 Pearson Education, Inc. All rights reserved. 23 Outline Card.cs

 2006 Pearson Education, Inc. All rights reserved. 24 Outline DeckOfCards.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 25 Outline DeckOfCards.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 26 Outline DeckOfCardsTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 27 Outline DeckOfCardsTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 28 Outline ForEachTest.cs

 2006 Pearson Education, Inc. All rights reserved. 29 Outline PassArray.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 30 Outline PassArray.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 31 Outline PassArray.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 32 Performance Tip 8.1 Passing arrays and other objects by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would waste time and would consume considerable storage for the copies of the arrays— both of these problems cause poor performance.

 2006 Pearson Education, Inc. All rights reserved. 33 Outline ArrayReferenceTest.cs (1 of 5)

 2006 Pearson Education, Inc. All rights reserved. 34 Outline ArrayReferenceTest.cs (2 of 5)

 2006 Pearson Education, Inc. All rights reserved. 35 Outline ArrayReferenceTest.cs (3 of 5)

 2006 Pearson Education, Inc. All rights reserved. 36 Outline ArrayReferenceTest.cs (4 of 5)

 2006 Pearson Education, Inc. All rights reserved. 37 Outline ArrayReferenceTest.cs (5 of 5)

 2006 Pearson Education, Inc. All rights reserved. 38 Software Engineering Observation 8.1 When a method receives a reference-type parameter by value, a copy of the object’s reference is passed. This prevents a method from overwriting references passed to that method. In the vast majority of cases, protecting the caller’s reference from modification is the desired behavior. If you encounter a situation where you truly want the called procedure to modify the caller’s reference, pass the reference-type parameter using keyword ref — but, again, such situations are rare.

 2006 Pearson Education, Inc. All rights reserved. 39 Software Engineering Observation 8.2 In C#, objects (including arrays) are passed by reference by default. So, a called method receiving a reference to an object in a caller can change the caller’s object.

 2006 Pearson Education, Inc. All rights reserved. 40 Outline GradeBook.cs (1 of 6)

 2006 Pearson Education, Inc. All rights reserved. 41 Outline GradeBook.cs (2 of 6)

 2006 Pearson Education, Inc. All rights reserved. 42 Outline GradeBook.cs (3 of 6)

 2006 Pearson Education, Inc. All rights reserved. 43 Outline GradeBook.cs (4 of 6)

 2006 Pearson Education, Inc. All rights reserved. 44 Outline GradeBook.cs (5 of 6)

 2006 Pearson Education, Inc. All rights reserved. 45 Outline GradeBook.cs (6 of 6)

 2006 Pearson Education, Inc. All rights reserved. 46 Software Engineering Observation 8.3 A test harness (or test application) is responsible for creating an object of the class being tested and providing it with data. This data could come from any of several sources. Test data can be placed directly into an array with an array initializer, it can come from the user at the keyboard, it can come from a file (as you will see in Chapter 18) or it can come from a network (as you will see in Chapter 23). After passing this data to the class’s constructor to instantiate the object, the test harness should call the object to test its methods and manipulate its data. Gathering data in the test harness like this allows the class to manipulate data from several sources.

 2006 Pearson Education, Inc. All rights reserved. 47 Outline GradeBookTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 48 Outline GradeBookTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 49 Fig | Rectangular array with three rows and four columns.

 2006 Pearson Education, Inc. All rights reserved. 50 Fig | Jagged array with three rows of different lengths.

 2006 Pearson Education, Inc. All rights reserved. 51 Outline InitArray.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 52 Outline InitArray.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 53 Outline InitArray.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 54 Outline GradeBook.cs (1 of 7)

 2006 Pearson Education, Inc. All rights reserved. 55 Outline GradeBook.cs (2 of 7)

 2006 Pearson Education, Inc. All rights reserved. 56 Outline GradeBook.cs (3 of 7)

 2006 Pearson Education, Inc. All rights reserved. 57 Outline GradeBook.cs (4 of 7)

 2006 Pearson Education, Inc. All rights reserved. 58 Outline GradeBook.cs (5 of 7)

 2006 Pearson Education, Inc. All rights reserved. 59 Outline GradeBook.cs (6 of 7)

 2006 Pearson Education, Inc. All rights reserved. 60 Outline GradeBook.cs (7 of 7)

 2006 Pearson Education, Inc. All rights reserved. 61 Outline GradeBookTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 62 Outline GradeBookTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 63 Outline VarargsTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 64 Outline VarargsTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 65 Common Programming Error 8.4 Using the params modifier with a parameter in the middle of a method parameter list is a syntax error. The params modifier may be used only with the last parameter of the parameter list.

 2006 Pearson Education, Inc. All rights reserved. 66 Outline InitArray.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 67 Outline InitArray.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 68 Outline InitArray.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 69 Fig | Collaborations in the ATM system.

 2006 Pearson Education, Inc. All rights reserved. 70 Fig | Communication diagram of the ATM executing a balance inquiry.

 2006 Pearson Education, Inc. All rights reserved. 71 Fig | Communication diagram for executing a BalanceInquiry.

 2006 Pearson Education, Inc. All rights reserved. 72 Fig | Sequence diagram that models a Withdrawal executing.

 2006 Pearson Education, Inc. All rights reserved. 73 Fig | Sequence diagram that models a Deposit executing.