1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 5 Arrays - Collections

31 375 0

Đ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

Arrays - Collections Chapter 5   !"# 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 )*#'+,'-!)*./0123  Allocate space for the array – number of elements in initializer list determines the size of array )*#'+,'-./0123 Slide 5 foreach Loops  Example: Slide 6 456'75(8#7 5'79 . ::5(8#7# 2 )*;','-./0123 4-353<<9 =>?4;',')*93 )*;','-./0123 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)  @# +,'493  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-35=?3<<9 ?=6@<-DD<)*3 ;%','493::%' ?=6@<-DED3 4-35=?3<<9 ?=6@<-DD<)*3 ;%'#4)C*93 ?=6@<-DE)C*;%'#D<)C*3 } (%;%','4)*9 . 4A-3A5=?3A<<9 )A*F-3 2 (%;%'#49 . 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?49#!  H?49## DataType[,]

Ngày đăng: 13/05/2014, 11:30

Xem thêm: Chapter 5 Arrays - Collections

TỪ KHÓA LIÊN QUAN

Mục lục

    Declaring and Allocating arrays

    Properties - Methods of an array

    Jagged Arrays (Arrays of arrays)

    Example: Defining Collections

    Hashtable: Gets all data

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN