Bài giảng C# 2010 - Chapter 4: Arrays and Collections

One Dimension Array

Multi Dimension Array

Array class

ArrayList class

Dictionary class

 

pptx68 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 1770 | Lượt tải: 5download
Tóm tắt nội dung Bài giảng C# 2010 - Chapter 4: Arrays and Collections, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 13/05/2013 ‹#› One Dimension Array 1 Multi Dimension Array 2 Array class 3 ArrayList class 4 Dictionary class 5 5 2 9 7 6 0 8 M[0] M[1] M[3] M[5] M[2] M[4] M[6] - Same name - Same type - Get Element by Index ( 0 n-1) One Dimension Array Programmer specifies the type of the elements of the array new operator to allocate dynamically the number of elements in the array Array declarations and initializations need not be in the same statement In arrays of value types, each element contains one value of the declared type Declaring & Creating Arrays [ ] ; int [ ] M; Button[ ] arrButton; M=new int[10]; Button []arrButton=new Button[10]; Declaring: =new [ ARRAY_SIZE]; Creating: int M1[]=new int[10]; for(int i=0;i Represents a collection of keys and values. Tkey: The type of the keys in the Dictionary. Tvalue: The type of the values in the Dictionary. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1). Every key in a Dictionary must be unique according to the dictionary's equality comparer A key cannot be null, but a value can be, if the value type TValue is a reference type The capacity is automatically increased as required by reallocating the internal array Use KeyValuePair structure representing a value and its key. Constructor Dictionary () Description: Initializes a new instance of the Dictionary class that is empty, has the default initial capacity, and uses the default equality comparer for the key type. // Create a new dictionary of ArrayList, with string keys. Dictionary 	myDIC = new Dictionary(); // Or Dictionary myDIC = new Dictionary (StringComparer.CurrentCultureIgnoreCase ); Method Add( TKey key, TValue value ) Description: Adds the specified key and value to the dictionary. ArrayList arr=new ArrayList(); arr.Add("Nhà");arr.Add("Chỗ ở"); myDIC.Add("Home",arr); Description: try {myDIC.Add("Home", null); } catch (ArgumentException ex) { MessageBox.Show(ex.Message); } Error: An item with the same key has already been added Using Indexer to get Value Item ArrayList aItem= myDIC["Home"]; foreach (KeyValuePair item in myDIC) { string strKey = item.Key; ArrayList arrValue=item.Value; } KeyValuePair Defines a key/value pair that can be set or retrieved Method public void Clear() Description: Removes all keys and values from the Dictionary . Ex: myDIC.Clear(); Method public bool ContainsKey(TKey key) Description: Determines whether the Dictionary contains the specified key. if (myDIC.ContainsKey("Home")) { MessageBox.Show(“Có home"); } Method public bool ContainsValue (TValue value ) Description: Determines whether the Dictionary contains a specific value if (myDIC.ContainsValue(arr)) {MessageBox.Show("Có Arr này rồi"); } Method public bool TryGetValue (TKey key, out TValue value ) Description: Gets the value associated with the specified key. ArrayList arr = null; if (myDIC.TryGetValue("Home", out arr)) {//Process arr here } else{//Could not Get value } Method public bool Remove(TKey key ) Description: Removes the value with the specified key from the Dictionary . myDIC.Remove("Home"); Properties Count Description: Gets the number of key/value pairs contained in the Dictionary . int nSize = myDIC.Count; Properties Item ( indexer) Description: Gets or sets the value associated with the specified key. KeyNotFoundException=>The property is retrieved and key does not exist in the collection. ArgumentNullException=>key is null ArrayList aItem= myDIC["Home"]; myDIC["Home"]=arr (Arraylist); Properties Keys Description: Gets a collection containing the keys in the Dictionary . Example: Dictionary. KeyCollection keyColl = myDIC.Keys; foreach (string s in keyColl) {//Process Key here } Properties Values Description: Gets a collection containing the values in the Dictionary . Example: Dictionary. ValueCollection valColl = myDIC.Values; foreach(ArrayList arr in valColl) {//Process value here } END 

File đính kèm:

  • pptxBài giảng C# 2010 - Chapter 4 Arrays and Collections.pptx
Tài liệu liên quan