Danh mục

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

Số trang: 69      Loại file: pptx      Dung lượng: 486.76 KB      Lượt xem: 8      Lượt tải: 0    
10.10.2023

Xem trước 7 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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,...
Nội dung trích xuất từ tài liệu:
Lecture Windows programming: Chapter 6 - Châu Thị Bảo HàArraysCollectionsChapter6 Ebook: Beginning Visual C# 2010, part 1, chapter 5, 11 Reference: CSharp How to ProgramContents Arrays Collections Slide 2Arrays(p.110) Declaring and Allocating arrays Initializing an array Properties – Methods of an array foreach loops Array parameter Multidimensional arrays Slide 3DeclaringandAllocatingarrays 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 4Initializinganarray 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 arrayint[] myIntArray = { 2, 4, 6, 8, 10}; Slide 5PropertiesMethodsofanarray 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 foreachLoops 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 7Examplestring 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 8Arrayparameter 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 9Arrayparameter: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 ){ SlideMultidimensionalArrays(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 SlideRectangulararrays Column0 Column Column2 Column3 1 Row0 a[0, 0] a[0, 1] a[0, 2] a[0, 3] Row1 a[1, 0] a[1, 1] a[1, 2] a[1, 3] Row2 a[2, 0] a[2, 1] a[2, 2] a[2, 3] Columnindex(orsubscript) Rowindex(orsubscript) Arrayname SlideRectangulararrays(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 s ...

Tài liệu được xem nhiều: