插入元素也是向集合中增加元素,与使用Add或AddRange方法添加元素不同的是,插入元素可以指定要插入的位置,而添加只能在集合的尾部按顺序添加。同样,插入元素也有两种方法:
- Insert方法将单个元素添加到指定的索引位置。
- InsertRange从指定的位置开始添加一个实现了ICollection接口的实例。
Module Module1
Sub Main()
Dim myAL As New ArrayList
myAL.Insert(0, "Everyone")
myAL.Insert(1, "Live")
myAL.Insert(2, "in")
myAL.Insert(3, "a")
myAL.Insert(4, "world")
myAL.Insert(5, "peace")
Dim myQueue As New Queue
myQueue.Enqueue("wants")
myQueue.Enqueue("to")
Console.WriteLine("ArrayList的初始内容是:")
printValues(myAL)
Console.WriteLine("队列Queue的初始内容是:")
printValues(myQueue)
'InsertRange
myAL.InsertRange(1, myQueue)
Console.WriteLine("添加队列后列表的内容是:")
printValues(myAL)
'Insert