Lecture Windows programming: Chapter 6 - Châu Thị Bảo Hà
lượt xem 1
download

Lecture Windows programming: Chapter 6 - Châu Thị Bảo Hà

Chapter 6 of lecture Windows programming introduce about Arrays - collections. In this chapter you will be learn contents: Declaring and Allocating arrays, initializing an array, properties – methods of an array, foreach loops, System.Collections namespace, ArrayList class,...
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Lecture Windows programming: Chapter 6 - Châu Thị Bảo Hà
- Arrays Collections Chapter 6 Ebook: Beginning Visual C# 2010, part 1, chapter 5, 11 Reference: CSharp How to Program
- Contents Arrays Collections Slide 2
- Arrays (p.110) Declaring and Allocating arrays Initializing an array Properties – Methods of an array foreach loops Array parameter Multidimensional arrays Slide 3
- Declaring and Allocating arrays Arrays are indexed lists of variables stored in a single array type variable DataType[ ] ArrayName; // declare array ArrayName = new DataType[size]; // allocate array // declare and allocate array DataType[ ] ArrayName = new DataType[size]; Example: int[] a = new int[12]; bool[] b = new bool[100]; Circle[] listCir = new Circle[5]; // reference types When arrays are allocated, the elements are initialized: zero for the numeric data-type false for bool variables null for reference types Slide 4
- Initializing an array Arrays can be declared, allocated, initialized in a statement int[] myIntArray = new int[5] { 2, 4, 6, 8, 10}; Allocate space for the array – number of elements in initializer list determines the size of array int[] myIntArray = { 2, 4, 6, 8, 10}; Slide 5
- Properties Methods of an array array_name.Length returns size of array array_name.GetValue (int index) array_name[int index] returns an object at position index array_name.SetValue (object value, int index) array_name[int index] = value modifies an object at position index Slide 6
- foreach Loops foreach ( in ) { // can use for each element } Example: int[] myArray = { 2, 4, 6, 8, string[] friendNames = { "Robert", "Mike", 10}; "Jeremy" }; foreach ( int x in myArray ) foreach (string friendName in friendNames) { Console.WriteLine( x ); Console.WriteLine( friendName ); Circle[] listC = new Circle[3] }{ new Circle(5.0), new Circle(7.0), new Circle(9.0) } foreach (Circle c in listC) { Console.WriteLine(c.Radius + " : " + c.getArea(); } Slide 7
- Example string output = ""; const int ARRAY_SIZE = 10; int[] x = new int[ 10 ]; int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; int[] z; z = new int[ ARRAY_SIZE ]; for ( int i = 0; i < z.Length; i++ ) z.SetValue(2 + 2 * i, i); output += "Subscript\tArray x\tArray y\tArray z\n"; for ( int i = 0; i < ARRAY_SIZE; i++ ) Change output += i + "\t" + x.GetValue(i) + "\t" + y.GetValue(i) + for to "\t" + z.GetValue(i) + "\n"; foreach loop? MessageBox.Show( output, "Initializing an array of int values", MessageBoxButtons.OK, MessageBoxIcon.Information ); Slide 8
- Array parameter No need the number of elements in the parameter array when you define the method Example: void InputArray (int[] a) void OutputArray (int[] a) int Sum (int[] arr) bool Search (int[] arr, int key) Pass arrays as arguments to methods by specifying the name of the array (no brackets) Example: InputArray(a); Arrays are passed by reference Individual array elements are passed by value Slide 9
- Array parameter: Example private void showOutputButton_Click( object sender, System.EventArgs e ) { int[] a = { 1, 2, 3, 4, 5 }; for ( int i = 0; i < a.Length; i++ ) outputLabel.Text += " " + a[ i ]; ModifyArray( a ); // passed by reference outputLabel.Text += "\n"; for ( int i = 0; i < a.Length; i++ ) outputLabel.Text += " " + a[ i ]; ModifyElement( a[ 3 ] ); outputLabel.Text += "\na[ 3 ] after ModifyElement: " + a[ 3 ]; } public void ModifyArray( int[] b ) { Slide
- Multidimensional Arrays (p.113) Require two or more subscripts to identify a particular element Arrays that require two subscripts to identify an element are called double-subscripted arrays rectangular arrays often represent tables jagged arrays (arrays of arrays) arrays that compose jagged arrays can be of different lengths Slide
- Rectangular arrays Column 0 Column Column 2 Column 3 1 Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3] Row 1 a[1, 0] a[1, 1] a[1, 2] a[1, 3] Row 2 a[2, 0] a[2, 1] a[2, 2] a[2, 3] Column index (or subscript) Row index (or subscript) Array name Slide
- Rectangular arrays (cont.) Declaring – Allocating: DataType[,] ArrayName; ArrayName = new DataType [rowSize, colSize]; DataType[,] ArrayName = new DataType [rowSize, colSize]; Example: int[,] b = new int[ 2, 2 ]; b[ 0, 0 ] = 1; b[ 0, 1 ] = 2; b[ 1, 0 ] = 3; b[ 1, 1 ] = 4; or: int[,] b = { { 1, 2 }, { 3, 4 } }; GetLength (int x): returns size of xth dimension in a array Slide
- Jagged arrays (arrays of arrays) Column 0 Column Column 2 Column 3 1 Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3] Row 1 a[1, 0] a[1, 1] Row 2 a[2, 0] a[2, 1] a[2, 2] Column index (or subscript) Row index (or subscript) Array name Slide
- Jagged arrays (cont.) Declaring – Allocating: DataType[ ][ ] ArrayName; ArrayName = new DataType[Size][ ]; DataType[ ][ ] ArrayName = new DataType[Size][ ]; Example: int[ ][ ] c = new int[2][ ]; c[0] = new int[] { 1, 2 }; c[1] = new int[] { 3, 4, 5 }; Length property: returns number of rows or columns c.Length 2 c[1] .Length 3 Slide
- Rectangular arrays: Example Slide
- Jagged arrays: Example Slide
- Example Slide
- Contents Arrays Collections Slide
- Collection classes Collection classes are used for maintaining lists of objects, and they may expose more functionality than simple arrays Collection classes are defined as part of the System.Collections or System.Collections.Generic namespace the collection classes in the System.Collections allow to create untyped collections store any type of object in the collection the collection classes in the System.Collections.Generic allow to create typed collections only store the specified data type the data type is specified in angle bracket () after name of the collection class Slide

CÓ THỂ BẠN MUỐN DOWNLOAD
-
Lecture Algorithm design - Chapter 6: Dynamic programming II
50 p |
28 |
2
-
Lecture Java: Chapter 6
62 p |
47 |
1
-
Lecture C++ programming: from problem analysis to program design - Chapter 6: User-defined functions I
39 p |
45 |
1
-
Lecture CIS 190: C++ programming - Chapter 6: Introduction to C++
65 p |
39 |
1
-
Lecture Algorithm design - Chapter 6: Dynamic programming I
37 p |
22 |
1
-
Lecture Routing Protocols - Chapter 6: Single-area OSPF
60 p |
31 |
1
-
Introduction to java programming: Chapter 6 - Arrays
40 p |
54 |
1
-
Lecture Windows programming: Chapter 8 - Châu Thị Bảo Hà
37 p |
30 |
1
-
Lecture Windows programming: Chapter 5 - Châu Thị Bảo Hà
29 p |
24 |
1
-
Lecture Windows programming: Chapter 7 - Châu Thị Bảo Hà
55 p |
36 |
1
-
Lecture Windows programming: Chapter 4 - Châu Thị Bảo Hà
72 p |
29 |
1
-
Lecture Windows programming: Chapter 3(3) - Châu Thị Bảo Hà
74 p |
26 |
1
-
Lecture Windows programming: Chapter 3(2) - Châu Thị Bảo Hà
33 p |
43 |
1
-
Lecture Windows programming: Chapter 3(1) - Châu Thị Bảo Hà
21 p |
27 |
1
-
Lecture Windows programming: Chapter 2 - Châu Thị Bảo Hà
68 p |
23 |
1
-
Lecture Windows programming: Chapter 1 - Châu Thị Bảo Hà
57 p |
40 |
1
-
Lecture Operating system: Chapter 6 - TS. Nguyễn Văn Hiệp
46 p |
21 |
1


Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline:0933030098
Email: support@tailieu.vn
