课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - ArrayList

当前位置:免费教程 » 程序设计 » VB.Net

ArrayList表示可以单独索引的对象的有序集合。 它基本上是一个数组的替代。 但是,与数组不同,您可以使用索引在指定位置从列表中添加和删除项目,并且数组会自动调整大小。 它还允许动态内存分配,添加,搜索和排序列表中的项目。


ArrayList类的属性和方法

下表列出了ArrayList类的一些常用属性:

属性描述
CapacityGets or sets the number of elements that the ArrayList can contain.
获取或设置ArrayList可以包含的元素数。
CountGets the number of elements actually contained in the ArrayList.
获取ArrayList中实际包含的元素数。
IsFixedSizeGets a value indicating whether the ArrayList has a fixed size.
获取指示ArrayList是否具有固定大小的值。
IsReadOnlyGets a value indicating whether the ArrayList is read-only.
获取一个值,该值指示ArrayList是否为只读。
ItemGets or sets the element at the specified index.
获取或设置指定索引处的元素。

下表列出了ArrayList类的一些常用方法:

S.N.方法名称和用途
1

Public Overridable Function Add (value As Object) As Integer

公共可覆盖函数Add(value As Object)As Integer

Adds an object to the end of the ArrayList.

将一个对象添加到ArrayList的末尾。

2

Public Overridable Sub AddRange (c As ICollection)

公共可覆盖Sub AddRange(c As ICollection)

Adds the elements of an ICollection to the end of the ArrayList.

将ICollection的元素添加到ArrayList的末尾。

3

Public Overridable Sub Clear

公共可覆盖Sub Clear

Removes all elements from the ArrayList.

从ArrayList中删除所有元素。

4

Public Overridable Function Contains (item As Object) As Boolean

公共可覆盖函数包含(item As Object) As Boolean

Determines whether an element is in the ArrayList.

确定元素是否在ArrayList中。

5

Public Overridable Function GetRange (index As Integer, count As Integer ) As ArrayList

ArrayList 作为公共可重写函数 GetRange (指数整数整数作为计数

Returns an ArrayList, which represents a subset of the elements in the source ArrayList.

返回一个ArrayList,它表示源ArrayList中的元素的子集。

6

Public Overridable Function IndexOf (value As Object) As Integer

公共可覆盖函数IndexOf(value As Object)As Integer
Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
返回ArrayList中或其一部分中值的第一次出现的从零开始的索引。

7

Public Overridable Sub Insert (index As Integer, value As Object)

公共可覆盖Sub Insert(index As Integer,value As Object)

Inserts an element into the ArrayList at the specified index.

在指定的索引处插入一个元素到ArrayList中。

8

Public Overridable Sub InsertRange (index As Integer, c As ICollection)

公共可覆盖ub InsertRange (指数整数c 作为集合)

Inserts the elements of a collection into the ArrayList at the specified index.

在指定的索引处将集合的元素插入ArrayList。

9

Public Overridable Sub Remove (obj As Object )

公共可覆盖Sub Remove (obj作为对象)

Removes the first occurrence of a specific object from the ArrayList.

从ArrayList中删除特定对象的第一次出现。

10

Public Overridable Sub RemoveAt (index As Integer)

公共可覆盖Sub RemoveAt (指数整数)

Removes the element at the specified index of the ArrayList.

删除ArrayList的指定索引处的元素。

11

Public Overridable Sub RemoveRange (index As Integer, count As Integer)

公共可覆盖Sub RemoveRange (指数整数整数作为计数

Removes a range of elements from the ArrayList.

 ArrayList 中移除某个范围元素

12

Public Overridable Sub Reverse

公共可覆盖Sub Reverse

Reverses the order of the elements in the ArrayList.

颠倒ArrayList中元素的顺序。

13

Public Overridable Sub SetRange (index As Integer, c As ICollection )

公共可覆盖Sub SetRange(指数整数c 作为集合

Copies the elements of a collection over a range of elements in the ArrayList.

将集合的元素复制到ArrayList中的一系列元素。

14

Public Overridable Sub Sort

公共可覆盖Sub 排序
Sorts the elements in the ArrayList.

在 ArrayList 元素进行排序

15

Public Overridable Sub TrimToSize

公共可覆盖Sub TrimToSize

Sets the capacity to the actual number of elements in the ArrayList.

将容量设置为ArrayList中的实际元素数。


示例:

下面的例子演示了这个概念:
  1. Sub Main()
  2. Dim al As ArrayList = New ArrayList()
  3. Dim i As Integer
  4. Console.WriteLine("Adding some numbers:")
  5. al.Add(45)
  6. al.Add(78)
  7. al.Add(33)
  8. al.Add(56)
  9. al.Add(12)
  10. al.Add(23)
  11. al.Add(9)
  12. Console.WriteLine("Capacity: {0} ", al.Capacity)
  13. Console.WriteLine("Count: {0}", al.Count)
  14. Console.Write("Content: ")
  15. For Each i In al
  16. Console.Write("{0} ", i)
  17. Next i
  18. Console.WriteLine()
  19. Console.Write("Sorted Content: ")
  20. al.Sort()
  21. For Each i In al
  22. Console.Write("{0} ", i)
  23. Next i
  24. Console.WriteLine()
  25. Console.ReadKey()
  26. End Sub
  27. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. Adding some numbers:
  2. Capacity: 8
  3. Count: 7
  4. Content: 45 78 33 56 12 23 9
  5. Content: 9 12 23 33 45 56 78
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号