在FreeBasic中使用集合類(泛型)(第三節(jié))
已有 2051 次閱讀2015-5-21 15:50
|個人分類:FreeBasic
接下來的內(nèi)容,可能會不斷地修正
Mdcollection接口的方法:
isEmpty() :判斷集合是否為空
contains(): 判斷集合中是否存在元素
remove():從集合中刪除一個對象的引用
clone(): 復(fù)制集合
size() :返回集合中元素的數(shù)目
equals():
hashCode():
retainAll():
toArray() :返回一個數(shù)組,該數(shù)組中包括集合中的所有元素
toString():
List接口的方法:
add(ByRef element) :向集合中加入一個元素
add(ByVal index As Integer, ByRef element) :指定位置,插入一個元素
addAll( ) :一次性插入多個元素
get(ByVal index As Integer) :按位置,返回一個元素
getClass() As String ?返回一個自定義類型,并轉(zhuǎn)成string
clear() :刪除集合中所有的對象,即不再持有這些對象的引用
indexOf() :返回元素在列表中的位置
lastindexof() :返回元素在列表中的最后的位置
listIterator() :返回迭代器
listIterator(ByVal index As Integer) :從指定位置開始迭代
set(ByVal index As Integer, ByRef element ): 替換元素值
subList(ByVal fromIndex As Integer, ByVal toIndex As Integer) :返回一個子列表
Vector接口的方法:(可以理解成動態(tài)列表?)
add(ByRef element) :向集合中加入一個元素
add(ByVal index As Integer, ByRef element) :指定位置,插入一個元素
addElement(ByRef element)
addAll( ) :一次性插入多個元素
capacity() As Integer :Vector的容量上限
copyInto(anArray() ) :復(fù)制成數(shù)組
elementAt(ByVal index As Integer) :按位置返回元素?
elements() :返回mdEnumeration
ensureCapacity(ByVal minCapacity As Integer) :自動調(diào)整容量以達到至少需要的容量
getClass() As String ?返回一個自定義類型,并轉(zhuǎn)成string
firstElement() :返回第一個元素
indexOf() :元素在Vector中的位置
insertElementAt() :在指定位置插入元素
lastElement() :返回最后一個元素
lastIndexOf :
removeAllElements() :刪除所有的元素
removeElement :刪除元素
removeElementAt :在指定位置刪除元素
trimToSize() :刪除空余的容量,以節(jié)約內(nèi)存
Map接口的方法:
equals():
getClass() As String
getKey():
getValue():
setValue:
toString():
使用MdList的例子
注意點:
1、mdCollection 是List類的基類。通常您應(yīng)當使用 mdList 而不是 mdCollection
2、mdSet 不允許重復(fù),而 mdList 則允許重復(fù)。
3、mdVector是一個有capacity(容量)的List表,如果數(shù)據(jù)達到容量的上限,必須手動調(diào)整容量,否則無法在Vector表中再增加元素
也就是說MdVector無法自動增加容量。('這個有點悲催呀')
4、mdStack(堆棧) 和 mdVector相似,但多增加一些方法。堆棧是一種"后進--先出"的list表
5、mdQueue (隊列)是一種先進--先出的list表
1、在Mdlist中使用基本數(shù)據(jù)類型的例子:
#include once "md/util/Mdlist.bi" '導(dǎo)入
mdListDeclare(string) '聲明List表的類型
dim list as mdList(string) '創(chuàng)建list對象
list.add("hallo")
list.add("word")
for i as integer=0 to list.size() -1
print list.get(i)
next
sleep
注意點1:get()方法是唯一可在list和它的子類中使用的方法?
注意點2:mdTypes支持所有freebasic的類型。但有一個數(shù)據(jù)類型integer要注意。在mdTypes中的一些方法也會使用integer,這樣容易導(dǎo)致與integer數(shù)據(jù)類型發(fā)生沖突,所以盡量用long代替integer
注意點3:mdListDeclare(類型),dim list as mdlist(類型),都是必須強制聲明。
2、list中元素是指針的例子:
#include once "md/util/mdList.bi"
mdListDeclare(Byte,Ptr)
dim list as mdList(byte,ptr)
dim as byte ptr a=new byte
dim as byte ptr b=new byte
*a=5
*b=7
list.add(a)
list.add(b)
for i as long=0 to list.size() -1
print *list.get(i)
delete list.get(i)
next
3、在list表中使用自定義類型UDT或自定義class
#Include Once "md/util/mdList.bi"
Type MyClass
Declare Constructor ()
Declare Operator Cast() As String
As Integer myVar
End Type
'Must have a default constructor or no explicit constructor at all!
Constructor MyClass ()
End Constructor
'Must implement cast to string for string representation
Operator MyClass.Cast() As String
Return Str(myVar)
End Operator
'Operator for comparision must exist!
Operator = (ByRef lhs As MyClass, ByRef rhs As MyClass) As Integer
Return lhs.myVar = rhs.myVar
End Operator
mdListDeclare(MyClass)
Dim As mdList(MyClass) list
Dim As MyClass foo
foo.myVar = 0 : list.add(foo)
foo.myVar = 1 : list.add(foo)
For i As Integer = 0 To list.size() - 1
Print list.get(i).myVar
Next
Sleep
注意點:1、自定義UDT,必須擁有一個空的構(gòu)造函數(shù)。
2、必須聲明擁有一個cast,強制返回字符串
3、必須擁有一個=比較符,能夠比較兩個自定義的對象