ArrayList类与List(of T)的用法差不多,提供的方法也差不多。
Add():在结尾处添加数据
RemoveAt():移除指定位置的数据
Insert():在指定位置处插入数据
Clear():移除所有元素
属性:
Count:返回包含的数据数量
与List(of T)不同的是,ArrayList可以包含任意类型的数据,但是相应的,要使用包含的数据,就必须对数据做转换。
如下例代码:
- 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)
- arrDate.Add(addString)
- arrDate.Add(addInteger)
- arrDate.Add(addDateTime)
- Console.WriteLine("包含数据数量:" & 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("数据1:{0}",toString)
- Console.WriteLine("数据2:{0}",toInteger)
- Console.WriteLine("数据3:{0}",toDateTime)
- Console.ReadKey()
- End Sub
运行结果如下:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录