集合類的系列化和反系列化 (注:本節(jié)內容很重要,在后面的數(shù)據(jù)庫對象映射,就依賴于集合類的系列化和反系列化,此內容要細細揣摩) 在使用集合時,您可能需要將集合中的內容序列化成字符串。所有的集合類都可以轉換為普通的字符串。你可以使用 Cast(操作符),Str(函數(shù)) 或 本庫中的tostring () 方法,很容易可以把集合中的內容序列化成字符串,但要將這些字符串反序列化到list或map或許更難一些。 mdCollectionsHelper 提供了一些靜態(tài)方法: (mdCollectionHelper自動引用了mdList.bi和mdMap.bi) createList(String) As mdList(String) :將字符串生成一個字符型list列表 createMap(String) As mdMap(String, String) :將字符串生成一個map字典 encode(String) As String :將一個普通字符串按區(qū)隔符"[]{}=,\r\n"等生成字符串 decode(String) As String :去掉區(qū)隔符生成一個普通字符串 下面的例子將演示如何進行集合類的系列化: #include once "md/helper/mdCollectionsHelper.bi" '先聲明好map的類型,mdCollectionsHelper很快會用到它 mdMapDeclare(string,string) '定義一些自定義類型(或類) type Myclass declare constructor() declare constructor( byref m as mdMap(string,string) declare operator cast() as string ‘必須聲明 label as string value as integer weight as double end type '聲明之后的代碼實現(xiàn) constructor myclass() end constructor '接下來的構造器用來初始化創(chuàng)建一個map對象,并將map的內容填入自定義類型的字段 constructor myClass(byref m as mdmap(string,string)) this.label=mdcollectionsHelper.decode(m.get("label") this.value=valint(m.get("value")) this.weight=val(m.get("weight")) end constructor operator myclass.cast() as string dim temp as string dim m as mdMap(string,string) temp=m.put("label",mdcollectionsHelper.encode(this.label)) ‘encode不是必須的,但對字符串encode也不是什么壞主意 temp=m.put("value",str(this.value)) temp=m.put("weight",str(this.weight)) return m end operator operator =(byref lhs as myclass,byref rhs as myclass) as boolean return str(lhs)=str(rhs) end operator '接下來開始實例化,并應用到具體的數(shù)據(jù)中 mdListDeclare(myclass) dim listofMyclass as mdlist(myclass) For i As Integer = LBound(firstArray) To UBound(firstArray) firstArray(i).label = "label" Str(i) firstArray(i).value = i firstArray(i).weight = i * 1.1 listOfMyClass.add(firstArray(i)) Next '讓我們看看我們創(chuàng)建的列表的輸出 — 事實上,我們可以通過網(wǎng)絡將數(shù)據(jù)發(fā)送出去,或將它保存到一個文件或任何需要的地方...... Dim As String stringRepresentation = listOfMyClass Print "List as string: " stringRepresentation Print '將字符串反序列回列表 Dim As mdList(String) listOfMaps = mdCollectionsHelper.createList(stringRepresentation) Dim As MyClass newArray(0 To listOfMaps.size() - 1) For i As Integer = LBound(newArray) To UBound(newArray) newArray(i) = TypeMyClass(mdCollectionsHelper.createMap(listOfMaps.get(i))) Print i, newArray(i) Next Sleep
Map接口使用的例子: mdMap 是默認的鍵值存儲。正常情況下,你不應當使用內部類 mdMapEntry,除非在mdMap中使用方法 entrySet()來返回 mdMapEntries mdSet。 mdDictionary 和 mdHashtable 和mdMap 幾乎是一樣的。 mdProperties 是一個對map的擴展類,mdProperties的鍵和值的類型總是字符串。(此類還沒有完全實現(xiàn)。) #Include Once "md/util/mdMap.bi" mdMapDeclare(String, String) Dim As mdMap(String, String) map Dim As String temp temp = map.put("key1", "value1") temp = map.put("key2", "value2") temp = map.put("key3", "value3") Dim As mdSet(String) set = map.keySet() Dim As String element ForEach(String, element In set) Print "key: " element " - value: " map.get(element) NextEach Sleep 使用mdIterator的例子: mdIterator 允許輕松地遍歷列表類。此外,它定義一個 ForEach 宏,允許使用類似 Java 的語法。 mdArrayIterator 擴展了 mdIterator 類 ,同樣也定義了ForEach 宏。使用它,可以遍歷 FreeBASIC 任何類型的數(shù)組。 mdEnumeration 跟 mdIterator 幾乎一樣。大多數(shù)時候,您將使用 mdIterator,但在某些場合下,你可能會用到mdEnumerations。 使用mdIterator的例子: #include once "md/util/mdlist.bi" mdListDeclare(string) '聲明類型 dim list as mdList(string) ‘聲明list對象類型 list.add("0") list.add("1") list.add("2") list.add("3") list.add("4") list.add("5") dim as mdIterator(string) it=list.iterator() '第一次迭代 while it.hasnext() print it.next() wend '第二次迭代 dim element as string foreach(string,element in list) print element nexteach sleep mdArrayIterator使用的例子: #include once "md/util/mdAarrayIterator.bi" mdAarrayIteratorDclare(string) dim array(0 to 9) for i as integer=0 to 9 array(i)=str(i) next dim as mdAarrayIterator(string) it=array() '首次迭代 while it.hasnext() print it.next() wend '第二次迭代 dim element as string foreach(string,element in array()) print element nexteach sleep mdEnumeration使用的例子: #include"md/util/mdEnumeration.bi" mdEnumerationDeclare(string) dim list as mdList(string) list.add("0") list.add("1") list.add("2") list.add("3") list.add("4") dim as mdenumeration(string) e=list while e.hasMoreElements() print e.nextElement wend sleep