在.NET Framework 2.0中的System.Collections namespace多了一個1.1所沒有的成員 – System.Collections.Generic
這個namespace最常用的應該是System.Collections.Generic.List<T>這個class(因為我常用:P)
而這個class能做什麼呢?! 其實你把他想成是個物件(或類別)的集合,可能就會比較容易理解
以前在VS6時代,要做物件集合的類別時,常常會使用以下方式來做(以vb6為例)

[vb] ‘ Filename: myClass.cls
Private String m_myValue = “”
Public Property Let myValue(ByVal vData As String)
m_myValue = vData
End Property

Public Property Get myValue() As String
myValue= m_myValue
End Property[/vb]

[vb]’ Filename: myClasses
Private mCol As New Collection

Public Sub Add(cls As myClass)
mCol.Add cls
End Sub

Public Function Insert(ByVal AfterIndexPos As Long, cls As myClass) As Long
If AfterIndexPos > mCol.Count Then Exit Function
If AfterIndexPos = 0 Then
mCol.Add cls, , 1
Insert = 1
Else
mCol.Add cls, , , AfterIndexPos
Insert = AfterIndexPos
End If
End Function

Public Property Get Item(ByVal Index As Long) As myClass
Set Item = mCol(Index)
End Property

Public Property Get Count() As Long
Count = mCol.Count
End Property

Public Sub Remove(ByVal Index As Long)
mCol.Remove Index
End Sub

Public Sub RemoveAll()
Dim i As Long
For i = 1 To mCol.Count
mCol.Remove 1
Next i
End Sub

Private Sub Class_Terminate()
Set mCol = Nothing
End Sub[/vb]
看吧!光一個集合, 就必須寫這麼一大串, 而假如程式內有一堆物件(或類別)集合時, 類似myClasses的程式就要寫好幾個
在.NET Framework 2.0中就不用這麼累了, 只要使用System.Collections.Generic.List<T>這個class, 只需一行程式, 因為諸如Add, Remove, Count這些Properties & methods, .Net Framework 2.0都已經幫我們做好了
以下就開始來說明System.Collections.Generic.List<T>的用法(以下將用C#說明)
[csharp]
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
class2 myText = new class2();
// 使用List.Add()方法來新增集合內容
myText.Values.Add(new class1(“123”));
myText.Values.Add(new class1(“456”));
myText.Values.Add(new class1(“ABC”));
myText.Values.Add(new class1(“DEF”));
myText.Values.Add(new class1(“Jaofeng”));
myText.Values.Add(new class1(“James”));

Predicate FindJaofeng = delegate(class1 obj) {
return obj.Value == “Jaofeng”;
};
Console.WriteLine(string.Format(“Jaofeng位於第 {0} 個”, myText.Values.FindIndex(FindJaofeng)));
Console.WriteLine(“=======================”);
Console.WriteLine(“將所有資料列出”);
int idx = 0;
Action
ListAll = delegate(class1 obj) {
Console.WriteLine(string.Format(“第 {0} 個的Value值為 {1}”, idx, obj.Value));
idx++;
};
myText.Values.ForEach(ListAll);
}
}

class class1 {

private string _value = string.Empty;

public string Value { get { return _value; } }
public class1(string value) {
_value = value;
}
}

class class2 {
private List _values = new List();

public List Values { get { return _values; } }

public class2() { }
}

}
[/csharp]

PS:請各位將以上範例的</class1>拿掉, 這是iG:Syntax Hiliter的Bug… @@”
執行的結果如下:
Jaofeng位於第 4 個
=======================
將所有資料列出
第 0 個的Value值為 123
第 1 個的Value值為 456
第 2 個的Value值為 ABC
第 3 個的Value值為 DEF
第 4 個的Value值為 Jaofeng
第 5 個的Value值為 James

當需要依條件來尋找集合內的某個類別時, 可用List<T>Find(), List<T>FindLast()來搜尋, 回傳搜尋到的類別
當需要依條件來尋找集合內的某些類別時, 可用List<T>FindAll()來搜尋, 將回傳一個新的List<T>物件集合
當需要依條件來尋找集合內的某個類別的索引值時, 可用List<T>FindIndex(), List<T>FindLastIndex()

List<T>Find(), List<T>FindLast()的不同是, List<T>Find()由Index=0開始尋找, 而List<T>FindLast()由Index = List<T>.Count – 1開始尋找
同理, List<T>FindIndex(), List<T>FindLastIndex()也是一樣, 不同的是, 這兩個回傳的是索引值

當使用List<T>Find()相關函示時, 必須delegate這個Predicate<T>
其內容就是搜尋的判斷式, 如:
[csharp]Predicate FindJaofeng = delegate(class1 obj) {
return obj.Value == “Jaofeng”;
};[/csharp]
return type為boolean值

而上面也有介紹一個List<T>.ForEach(), 這個Method只是將原本我們用foreach()的方式, 簡化而已
譬如原本的習慣寫法:
[csharp]foreach (class1 cls in myText.Values) {
// … Do something
}
// 現在變成
Action ActionName = delegate(class1 obj) {
// … Do something
};
myText.Values.ForEach(ActionName);
[/csharp]
程式碼雖然看起來好像沒比較簡化, 但是執行效率卻是比習慣寫法的效率高喔…

最後修改日期: 2005-09-29

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。