Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 31 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
31
Dung lượng
836,5 KB
Nội dung
Arrays-CollectionsChapter5 !"# Contents Arrays Collection classes Slide 2 Arrays (p.110) Declaring and Allocating arrays Initializing an array foreach Loops Properties – Methods of an array Array parameter Multidimensional arrays Slide 3 Declaring and Allocating arrays int[] a; a = new int[12]; string[] b = new string[100]; double[] array1 = new double[10], array2 = new double[20]; When arrays are allocated, the elements are initialized: $#%&' ( ' DataType[ ] ArrayName; // declare array ArrayName = new DataType[size]; // allocate array // declare and allocate array DataType[ ] ArrayName = new DataType[size]; Slide 4 Initializing an array Arrays can be declared, allocated, initialized in a statement )*#'+,'-!)*./0123 Allocate space for the array – number of elements in initializer list determines the size of array )*#'+,'-./0123 Slide 5 foreach Loops Example: Slide 6 456'75(8#7 5'79 . ::5(8#7# 2 )*;','-./0123 4-353<<9 =>?4;',')*93 )*;','-./0123 4@;','9 =>?4@93 Properties - Methods of an array array_name.Length $' array_name.GetValue (int index) array_name[int index] A%@ array_name.SetValue (object value, int index) array_name[int index] = value #%A%@ 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++ ) output += i + "\t" + x.GetValue(i) + "\t" + y.GetValue(i) + "\t" + z.GetValue(i) + "\n"; 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 @# (%+,'4)*9 (%B,'4)*9 #4)*9 4)*'9 Pass arrays as arguments to methods by specifying the name of the array (no brackets) @# +,'493 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 ) { )*-.C/23 4-35=?3<<9 ?=6@<-DD<)*3 ;%','493::%' ?=6@<-DED3 4-35=?3<<9 ?=6@<-DD<)*3 ;%'#4)C*93 ?=6@<-DE)C*;%'#D<)C*3 } (%;%','4)*9 . 4A-3A5=?3A<<9 )A*F-3 2 (%;%'#49 . F-3 2 Slide 10 [...]...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 11 Rectangular arrays Column 0 Column Column... ArrayList for( int i = 0; i < 5; i++) { intArray.Add( r.Next(10) ); } // xuất ArrayList foreach( int x in intArray ) { Console.Write( x+ " " ); } // tìm kiếm trong ArrayList int k =5; if (intArray.Contains(k)) Console.WriteLine("Co {0} trong mang", k); } Slide 22 Example: Defining Collections Student 0 * - id: String - name: String - address: String + ToString(): String 1 Students - list: ArrayList... int[] { 3, 4, 5 }; Length property: returns number of rows or columns c.Length 2 c[1] Length 3 Slide 15 Example: Rectangular arrays Slide 16 Example: Jagged Arrays Slide 17 Example Slide 18 Contents Arrays Collection classes Slide 19 Collection classes Collection classes in general are used for maintaining lists of objects, and they may expose more functionality than simple arrays Some... returns number of rows GetLength(1) returns number of columns Slide 13 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 14 Jagged Arrays (cont.) Declaring – Allocating: DataType[ ][ ] ArrayName; ArrayName = new DataType[Size][... Collection classes in general are used for maintaining lists of objects, and they may expose more functionality than simple arrays Some class in System .Collections namespace: ArrayList Hashtable Queue Stack Slide 20 ArrayList: Properties-Methods Property/Method Description Add (object value) Adds an object to the ArrayList Insert (int index, object value) Inserts an object at the index Remove... ICollection of all the keys stored in the Hashtable Remove (object obj) Removes the first occurrence of the object Count Gets the number of elements Clear() Removes all the elements Slide 25 Hashtable: Properties-Methods Property/Method Description Contains (object item) Returns true if the specified object is in the Hashtable; otherwise, returns false ContainsKey(object key) Determines whether the... two dimensional array and have a link format of Key and Value For example, a person name and his social security number and we can access there data using a key Key Value Slide 24 Hashtable: Properties-Methods Property/Method Description Add (object key, object value) Adds an object to the Hashtable with the specified key and value into the Hashtable Values Gets an ICollection of all the values stored... 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 12 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 . ] ArrayName; // declare array ArrayName = new DataType[size]; // allocate array // declare and allocate array DataType[ ] ArrayName = new DataType[size]; Slide 4 Initializing an array Arrays. (p.110) Declaring and Allocating arrays Initializing an array foreach Loops Properties – Methods of an array Array parameter Multidimensional arrays Slide 3 Declaring and Allocating arrays int[] a; a. subscript) Array name a[2, 1] Slide 12 Rectangular arrays (cont.) Declaring – Allocating: Example: )*-!) *3 )*- 3 )*- 3 )*-C3)*- /3 )*-..2.C/2 23 H?4@9$@ %#' H?49#! H?49## DataType[,]