Tài liệu Module 6: Arrays docx

50 402 0
Tài liệu Module 6: Arrays docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Module 6: Arrays Contents Overview Overview of Arrays Creating Arrays 10 Using Arrays 17 Lab 6.1: Creating and Using Arrays 29 Review 40 Information in this document, including URL and other Internet Web site references, is subject to change without notice Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, places or events is intended or should be inferred Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property  2001−2002 Microsoft Corporation All rights reserved Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BizTalk, IntelliSense, JScript, MSDN, PowerPoint, SQL Server, Visual Basic, Visual C++, Visual C#, Visual J#, Visual Studio, and Win32 are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries The names of actual companies and products mentioned herein may be the trademarks of their respective owners Module 6: Arrays iii Instructor Notes Presentation: 45 Minutes Lab: 60 Minutes This module describes how to declare and use arrays of varying ranks in C# The module is intentionally limited to rectangular arrays and does not cover ragged arrays int[ ] row; int[,] grid; int[,,] cube; // Covered // Covered // Covered int[ ][ ] grid; // // int[ ][ ][ ] cube1; // // int[ ][,] cube2; // // Ragged arrays CLS complaint Ragged arrays CLS complaint Ragged arrays CLS complaint are and are and are and no longer considered are not covered no longer considered are not covered no longer considered are not covered The module begins with explanations of the basic concepts of arrays, including notation, variables, and rank It provides the syntax for declaring an array variable, for accessing an array element, and for checking array bounds Note that the syntax for creating an array instance is covered in the next section The second section explains how to create and initialize arrays The third section describes how to use array properties and methods, how to pass arrays as parameters, how to return arrays from methods, how to use command-line arguments to Main, and how to use foreach statements on arrays In Exercise 1, students write a program that expects the name of a text file as an argument to Main and then reads the contents of the named text file into an array of characters It then iterates through the array of characters, classifying each character as a vowel or a consonant Finally, the program summarizes the contents of the array by printing a short report to the console In Exercise 2, students create and use arrays of rank and use the Array.GetLength(int dimension) method After completing this module, students will be able to:  Create, initialize, and use arrays of varying rank  Use command-line arguments in a C# program  Understand the relationship between an array variable and an array instance  Use arrays as method parameters  Return arrays from methods iv Module 6: Arrays Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module Required Materials To teach this module, you need the following materials:  Microsoft® PowerPoint® file 2124C_06.ppt  Module 6, “Arrays”  Lab 6, Creating and Using Arrays Preparation Tasks To prepare for this module, you should:  Read all of the materials for this module  Complete the lab  Read the instructor notes and the margin notes for the module  Practice the demonstration Module 6: Arrays Demonstration This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes Arguments for Main  To prepare for the demonstration • Using a text editor, type in the following code and save it to a file called CommandLine.cs using System; class Demonstration { static void Main(string[ ] args) { Console.WriteLine("using for:"); for (int i = 0; i < args.Length; i++) { Console.WriteLine("{0} {1}", i, args[i]); } } }  To demonstrate using arguments for Main Using a text editor, show the code for CommandLine.cs and point out the for loop Open the Visual Studio.NET Command Prompt and compile CommandLine.cs: C:> csc /t:exe CommandLine.cs From the command line, run CommandLine.exe, supplying parameters as shown below: C:> CommandLine Gillingham FC rule OK Verify that the output is as follows: using for: Gillingham FC rule OK v vi Module 6: Arrays Switch back to the text editor, and change the program to use a foreach statement rather than a for statement, as follows: using System; class Demonstration { static void Main(string[ ] args) { Console.WriteLine("using foreach:"); foreach (string arg in args) { Console.WriteLine(arg); } } } Recompile CommandLine.cs from the command prompt: C:> csc /t:exe CommandLine.cs From the command line, rerun CommandLine.exe, supplying parameters as shown below: C:> CommandLine Hello World Verify that the output is as follows: Using foreach: Hello World Module 6: Arrays vii Module Strategy Use the following strategy to present this module:  Overview of Arrays When presenting this section, consider the following issues: • Do not use array creation expressions in your examples (These expressions are covered in the next section.) • Exceptions from out-of-bounds access attempts are not the focus of this module • Mention that ragged arrays are no longer considered CLS compliant types • In Exercise of the lab, students are required to use constructor arguments when creating FileStream and StreamReader objects The lab instructions point students to the online Help files because constructors have not yet been covered  Creating Arrays This section provides the opportunity to focus on general array issues that students should be aware of before delving into specific implementation details It covers basic array concepts such as element types, rank, and indexing It also provides the syntax for creating, initializing, and accessing arrays in C#  Using Arrays • New concepts This section introduces the new operator Do not go into too much detail in comparing reference types to value types, however, because this is covered in a later module • Slides The slides in this section focus on the syntax required in the lab The methods named in the Commonly Used Methods slide are not used in the lab Mention that some of them are static methods Methods will be covered in detail later in the course The slides not show three-dimensional arrays, so you will need to show examples on a white board or flip chart • Examples Consider giving an example of an array of struct type Creating an array of 100 structs requires a single allocation, whereas creating an array of 100 objects requires 101 allocations―one for the array and 100 for the elements Module 6: Arrays Overview Topic Objective To provide an overview of the module topics and objectives Lead-in In this module, you will review the basic concept of arrays You will learn how to create, initialize, and use arrays  Overview of Arrays  Creating Arrays  Using Arrays *****************************ILLEGAL FOR NON-TRAINER USE****************************** Arrays provide an important means for grouping data To make the most of C#, it is important to understand how to use and create arrays effectively After completing this module, you will be able to:  Create, initialize, and use arrays of varying rank  Use command-line arguments in a C# program  Understand the relationship between an array variable and an array instance  Use arrays as parameters for methods  Return arrays from methods Module 6: Arrays  Overview of Arrays Topic Objective To provide an overview of the topics covered in this section Lead-in This section provides an overview of array concepts, core features, and basic syntax  What Is an Array?  Array Notation in C#  Array Rank  Accessing Array Elements  Checking Array Bounds  Comparing Arrays to Collections *****************************ILLEGAL FOR NON-TRAINER USE****************************** This section provides an overview of general array concepts, introduces the key syntax used to declare arrays in C#, and describes basic array features such as rank and elements In the next section, you will learn how to define and use arrays ... respective owners Module 6: Arrays iii Instructor Notes Presentation: 45 Minutes Lab: 60 Minutes This module describes how to declare and use arrays of varying ranks in C# The module is intentionally... arrays as method parameters  Return arrays from methods iv Module 6: Arrays Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module. .. output is as follows: Using foreach: Hello World Module 6: Arrays vii Module Strategy Use the following strategy to present this module:  Overview of Arrays When presenting this section, consider

Ngày đăng: 10/12/2013, 16:16

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan