ArrayList類與List(of T)的用法差不多,提供的方法也差不多。
Add():在結(jié)尾處添加數(shù)據(jù)
RemoveAt():移除指定位置的數(shù)據(jù)
Insert():在指定位置處插入數(shù)據(jù)
Clear():移除所有元素
屬性:
Count:返回包含的數(shù)據(jù)數(shù)量
與List(of T)不同的是,ArrayList可以包含任意類型的數(shù)據(jù),但是相應的,要使用包含的數(shù)據(jù),就必須對數(shù)據(jù)做轉(zhuǎn)換。
如下例代碼:
- Sub Main()
- Dim arrDate As New ArrayList
- Dim addString As String = "this is a test"
- Dim addInteger As Integer = 123
- Dim addDateTime As New DateTime(2017, 3, 20, 10, 10, 10)
- arrDate.Add(addString)
- arrDate.Add(addInteger)
- arrDate.Add(addDateTime)
- Console.WriteLine("包含數(shù)據(jù)數(shù)量:" & arrDate.Count)
- Dim toString As String = CType(arrDate(0), String)
- Dim toInteger As Integer = CType(arrDate(1), Integer)
- Dim toDateTime As DateTime = CType(arrDate(2), DateTime)
- Console.WriteLine("數(shù)據(jù)1:{0}", toString)
- Console.WriteLine("數(shù)據(jù)2:{0}", toInteger)
- Console.WriteLine("數(shù)據(jù)3:{0}", toDateTime)
- Console.ReadKey()
- End Sub
運行結(jié)果如下:
由于.net平臺下C#和vb.NET很相似,本文也可以為C#愛好者提供參考。
學習更多vb.net知識,請參看 vb.net 教程 目錄