intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Winform - Lập Trình C# - Lập Trình C Shap - Unlicensed Collections

Chia sẻ: No Comment | Ngày: | Loại File: PPT | Số trang:28

221
lượt xem
72
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'winform - lập trình c# - lập trình c shap - unlicensed collections', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Winform - Lập Trình C# - Lập Trình C Shap - Unlicensed Collections

  1. Collections Collections 1 BM HTTT - Khoa CNTT - HUI
  2. ̣ Nôi dung Nôi Array và ArrayList  Queue và Stack  Môt số interface cua System.Collections ̣ ̉  2
  3. System.Array và System.Collections System.Array Array trong C# được thực thi như 1 instance cua lớp ̉  System.Array và là 1 trong cac loai lớp collection ́ ̣ Han chê: Có kich cỡ cố đinh, nên không thể thêm muc mới ̣ ́ ́ ̣ ̣  ̀ ́ ̉ vao cuôi mang Cac lớp Collection được dung để thao tac danh sach cac ́ ̀ ́ ́ ́  đôi tượng (list of objects) và có thể triên khai nhiêu chức ́ ̉ ̀ năng hơn mang thông thường. ̉ Cac chức năng nay được thực thi thông qua cac interface ́ ̀ ́  trong namespace System.Collections. Lớp thực thi cac ́ interface nay có thể theo những cach khac với ̀ ́ ́ System.Array. 3
  4. Class Array Class Tât cả mang đêu kế thừa ngâm đinh từ lớp trừu tượng ́ ̉ ̀ ̀ ̣  Array (namespace System) Property Length: specifies the number of elements in the  array. Class Array provides static methods that provide  algorithms for processing arrays: Sort  Copy  4
  5. Cac han chế cua mang thông thường Cac ̣ ́ ̉ ̉ Khi muôn đinh lai kich cỡ mang thông thường, ́ ̣ ̣́ ̉  thường phai tao 1 mang mới, copy cac phân tử ̣̉ ̉ ́ ̀ cân giữ lai, câp nhât cac tham chiêu đên mang ̀ ̣ ̣ ̣ ́ ́ ́ ̉ gôc sao cho nó cung tham chiêu đên mang mới. ́ ̃ ́ ́ ̉ Nêu muôn xoa 1 phân tử khoi mang, phai ́ ́ ́ ̀ ̉ ̉ ̉  chuyên tât cả cac phân tử sau phân tử bị xoa lên ̉ ́ ́ ̀ ̀ ́ 1 vị trí Khi muôn chen 1 phân tử vao mang, phai ́ ̀ ̀ ̀ ̉ ̉  chuyên cac phân tử xuông 1 vị trí để tao ô trông ̉ ́ ̀ ́ ̣ ́ cho phân tử cân chen  khi đó phân tử cuôi sẽ ̀ ̀ ̀ ̀ ́ bị mât ́ 5
  6. Collection Overview Collection Tât cả cac lớp collection trong .NET Framework đêu thực ́ ́ ̀  thi từ 1 tổ hợp cac collection interfaces. ́ Cac collection interface khai bao cac operations sẽ được ́ ́ ́  thực thi trên cac loai collection khac nhau. ́ ̣ ́ Tât cả interface khai bao trong namespace ́ ́  System.Collections đêu có 1 loai generic tương đông ̀ ̣ ̀ trong namespace System.Collections.Generic. 6
  7. Môt số Collection nongeneric thông dung ̣ ̣ Môt ArrayList  Represents a dynamically size array of objects  Hashtable  Represents a collection of objects identified by a numerical  Queue  Represents a standard FIFO queue  SortedList  Like a dictionary  The elements can be accessed by ordinal position  Stack  A FIFO queue providing push and pop( and peek) functionality  7
  8. Lớp ArrayList Là lớp thông dung dung để săp xêp lai cac phân tử bên ̣ ̀ ́ ́ ̣ ́ ̀  ̉ trong 1 mang. Thực thi giao diên IList và dung để tao mang có kich cỡ ̣ ̀ ̣ ̉ ́  ̣ ̉ ̀́ đông (tăng giam tuy y) ArrayList châp nhân giá trị null như 1 giá trị hợp lệ và cho ́ ̣  phep cac phân tử có giá trị trung nhau. ́ ́ ̀ ̀ Demo Project ArrayListTest  8
  9. Môt ̀ ̣ ̣ ̉ Môt vai method thông dung cua ArrayList Add : thêm 1 đôi tượng vao cuôi ArrayList. ́ ̀ ́  CopyTo(Array) : copy toan bộ arrayList vao mang 1 chiêu. ̀ ̀ ̉ ̀  Equals(Object) xac đinh Object có băng với đôi tượng ́ ̣ ̀ ́  ̣ ̀ ̉ hiên hanh cua ArrayList hay không GetEnumerator () Returns an enumerator for the entire  ArrayList. IndexOf(Object) Searches for the specified Object and  returns the zero-based index of the first occurrence within the entire ArrayList. Insert Inserts an element into the ArrayList at the specified  index. 9
  10. Môt ̀ ̣ ̣ ̉ Môt vai method thông dung cua ArrayList Remove Removes the first occurrence of a specific object  from the ArrayList. RemoveAt Removes the element at the specified index of  the ArrayList. Clear Removes all elements from the ArrayList.  Reverse () Reverses the order of the elements in the entire  ArrayList. Sort () Sorts the elements in the entire ArrayList.  10
  11. Example Example Our ArrayList maintains a set of Car objects  class Car { // Public fields for simplicity. public string PetName; public int Speed; // Constructors. public Car(){} public Car(string name, int currentSpeed) { PetName = name; Speed = currentSpeed;} } 11
  12. static void ArrayListTest() { Console.WriteLine("\n=> ArrayList Test:\n"); ArrayList carArList = new ArrayList(); carArList.AddRange(new Car[] { new Car("Fred", 90, 10), new Car("Mary", 100, 50), new Car("MB", 190, 11)}); Console.WriteLine("Items in carArList: {0}", carArList.Count); // Print out current values. foreach(Car c in carArList) Console.WriteLine("Car pet name: {0}", c.PetName); Console.WriteLine("->Inserting new Car."); carArList.Insert(2, new Car("TheNewCar", 0, 12)); Console.WriteLine("Items in carArList: {0}", carArList.Count); object[] arrayOfCars = carArList.ToArray(); for(int i = 0; i < arrayOfCars.Length; i++) { Console.WriteLine("Car pet name: {0}", ((Car)arrayOfCars[i]).PetName); }} 12
  13. Queue Queue Theo cơ chế first-in, first-out (FIFO)  ́ ̣ ̉ Cac method thông dung cua Queue :  back: Returns a reference to the last and most recently  added element at the back of the queue. empty:Tests if the queue is empty.  front: Returns a reference to the first element at the front  of the queue. pop: Removes an element from the front of the queue.  push: Adds an element to the back of the queue.  size: Returns the number of elements in the queue.  13
  14. Stack Stack Thực thi cơ chế Last-in First out (LIFO)  ́ ̣ ̉ Cac method thông dung cua Stack:  GetEnumerator Returns an IEnumerator for the Stack.  GetType Gets the Type of the current instance.  Peek Returns the object at the top of the Stack without  removing it. Pop Removes and returns the object at the top of the Stack.  Push Inserts an object at the top of the Stack  14
  15. So sanh Array và collection So ́ Array Collection ̉ ́ ̣ ̀ ́ Phai khai bao loai Không cân khai bao cho cac phân tử ́ ̀ Kich thước mang là ́ ̉ Có kich thước đông ́ ̣ cố đinh ̣ Có nhiêu hơn 1 chiêu ̀ ̀ Chỉ có 1 chiêu ̀ 15
  16. Common collection interfaces Common 16
  17. 17
  18. 18
  19. IEnumerator interface IEnumerator IEnumerator là lớp cơ ban cho tât cả nongeneric ̉ ́  enumerators  IEnumerator < T > là lớp tương đương ̉ cho phiên ban generic Enumerators được dung để đoc (nhưng không thể chinh ̀ ̣ ̉  sửa ) dữ liêu trong collection. ̣ Enumerator luc đâu được đinh vị ở trước phân tử đâu tiên ́ ̀ ̣ ̀ ̀  ̉ cua collection. 19
  20. IEnumerator interface IEnumerator Cac lớp Collection có thể tao cac bộ liêt kê (enumerators) ́ ̣ ́ ̣  khac nhau để duyêt qua cac phân tử cua collection. ́ ̣ ́ ̀ ̉ Tuy có nhiêu cach thực thi enumerators nay nhưng tât cả ̀ ́ ̀ ́  đêu xuât phat từ IEnumerator interface nhờ đó có thể xử ̀ ́ ́ lý chung 1 cach đa hinh. ́ ́ ̀ 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2