1. Trang chủ
  2. » Công Nghệ Thông Tin

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

28 423 1

Đ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

Collections Collections 1 BM HTTT - Khoa CNTT - HUI Nội dung Nội dung  Array và ArrayList  Queue và Stack  Một số interface của System.Collections 2 System.Array và System.Collections System.Array và System.Collections  Array trong C# được thực thi như 1 instance của lớp System.Array và là 1 trong các loại lớp collection  Hạn chế: Có kích cỡ cố định, nên không thể thêm mục mới vào cuối mảng  Các lớp Collection được dùng để thao tác danh sách các đối tượng (list of objects) và có thể triển khai nhiều chức năng hơn mảng thông thường.  Các chức năng này được thực thi thông qua các interface trong namespace System.Collections. Lớp thực thi các interface này có thể theo những cách khác với System.Array. 3 Class Array Class Array  Tất cả mảng đều kế thừa ngầm định 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 Các hạn chế của mảng thông Các hạn chế của mảng thông thường thường  Khi muốn định lại kích cỡ mảng thông thường, thường phải tạo 1 mảng mới, copy các phần tử cần giữ lại, cập nhật các tham chiếu đến mảng gốc sao cho nó cũng tham chiếu đến mảng mới.  Nếu muốn xóa 1 phần tử khỏi mảng, phải chuyển tất cả các phần tử sau phần tử bị xóa lên 1 vị trí  Khi muốn chèn 1 phần tử vào mảng, phải chuyển các phần tử xuống 1 vị trí để tạo ô trống cho phần tử cần chèn  khi đó phần tử cuối sẽ bị mất 5 Collection Overview Collection Overview  Tất cả các lớp collection trong .NET Framework đều thực thi từ 1 tổ hợp các collection interfaces.  Các collection interface khai báo các operations sẽ được thực thi trên các loại collection khác nhau.  Tất cả interface khai báo trong namespace System.Collections đều có 1 loại generic tương đồng trong namespace System.Collections.Generic. 6 Một số Collection nongeneric thông Một số Collection nongeneric thông dụng dụng  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 Lớp ArrayList Lớp ArrayList  Là lớp thông dụng dùng để sắp xếp lại các phần tử bên trong 1 mảng.  Thực thi giao diện IList và dùng để tạo mảng có kích cỡ động (tăng giảm tùy ý)  ArrayList chấp nhận giá trị null như 1 giá trị hợp lệ và cho phép các phần tử có giá trị trùng nhau.  Demo Project ArrayListTest 8 Một vài method thông dụng của ArrayList Một vài method thông dụng của ArrayList  Add : thêm 1 đối tượng vào cuối ArrayList.  CopyTo(Array) : copy toàn bộ arrayList vào mảng 1 chiều.  Equals(Object) xác định Object có bằng với đối tượng hiện hành của 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 Một vài method thông dụng của ArrayList Một vài method thông dụng của 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 [...]... name/value IDictionaryEnumerator Ienumerable: trả về interface IEnumerator cho 1 đối tượng Ienumerator: cho phép lập lại theo kiểu foreach đối với ca c kiểu con IHashCodeProvider Ilist: cung c ́p hành động thêm, xóa, và lấy chỉ mu c ca c mu c trong 1 danh sách ca c đối tượng 22 ICollection interface   Là interface c bản cho ca c class trong System .Collections namespace ICollection... trỏ đến ca c phần tử trong danh sách Lu c khởi đầu, con trỏ nằm trươ c phần tử đầu tiên  21 Interface of the System .Collections Namespace         Icollection: xa c định ca c đă c tính của tất cả loại Collection nongeneric Icomparer : cho phép 2 đối tượng so sánh với nhau Idictionary: cho phép đối tượng collection nongeneric biểu diễn nội dung của nó bằng c ̣p name/value... tương đương cho phiên bản generic Enumerators đươ c dùng để đo c (nhưng không thể chỉnh sửa ) dữ liệu trong collection Enumerator lu c đầu đươ c định vị ở trươ c phần tử đầu tiên của collection 19 IEnumerator interface   Ca c lớp Collection có thể tạo ca c bộ liệt kê (enumerators) kha c nhau để duyệt qua ca c phần tử của collection Tuy có nhiều cách thư c thi enumerators... Stack Push Inserts an object at the top of the Stack 14 So sánh Array và collection Array Phải khai báo loại cho ca c phần tử Collection Không c ̀n khai báo Kích thươ c mảng là c ́ định Có kích thươ c động Có nhiều hơn 1 chiều Chỉ có 1 chiều 15 Common collection interfaces 16 17 18 IEnumerator interface    IEnumerator là lớp c bản cho tất cả nongeneric enumerators  IEnumerator... System .Collections. Generic Namespace  Contains numerous class and interface types that allow you to contain subitems in a variety of containers  Similar to System .Collections but more general  Differences: • T keyword: used to represent types • TKey keyword: used for keys • TValue: used for values 27 System .Collections System .Collections. generic ICollection ICollection IDictionary IDictionary... 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();... 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 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",... public interface ICollection : IEnumerable { int Count { get; } bool IsSynchronized { get; } object SyncRoot { get; } void CopyTo(Array array, int index); IEnumerator GetEnumerator(); } 23 The Role of IDictionary  IDictionary biểu diễn 1 tập hợp ca c cặp name/value public interface IDictionary : ICollection, IEnumerable { bool IsFixedSize { get; } bool IsReadOnly { get; } // Type indexer object... tất cả đều xuất phát từ IEnumerator interface nhờ đó có thể xử lý chúng 1 cách đa hình 20 IEnumerable interface IEnumerable interface chứa duy nhất 1 method IEnumerator GetEnumerator(); Method này trả về đối tượng enumerator thư c thi interface System .Collections. IEnumerator  Đối tượng enumerator đươ c dùng để duyệt qua từng phần tử của collection  Enumerator như 1 con... the back of the queue size: Returns the number of elements in the queue 13 Stack   Thư c thi c chế Last-in First out (LIFO) Ca c method thông dụng của 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 . tử cuối sẽ bị mất 5 Collection Overview Collection Overview  Tất cả ca c lớp collection trong .NET Framework đều thư c thi từ 1 tổ hợp ca c collection interfaces.  Ca c collection. này đươ c thư c thi thông qua ca c interface trong namespace System .Collections. Lớp thư c thi ca c interface này có thể theo những cách kha c với System.Array. 3 Class Array Class Array  Tất. System .Collections  Array trong C# đươ c thư c thi như 1 instance của lớp System.Array và là 1 trong ca c loại lớp collection  Hạn chế: Có kích c ̃ c ́ định, nên không thể thêm mục

Ngày đăng: 12/07/2014, 02:20

Xem thêm: Winform - Lập Trình C# - Lập Trình C Shap - Unlicensed Collections pot

TỪ KHÓA LIÊN QUAN