VB.Net - ArrayList

由 youj 创建, 最后一次修改 2016-12-16

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中的实际元素数。


示例:

下面的例子演示了这个概念:
Sub Main()
      Dim al As ArrayList = New ArrayList()
      Dim i As Integer
      Console.WriteLine("Adding some numbers:")
      al.Add(45)
      al.Add(78)
      al.Add(33)
      al.Add(56)
      al.Add(12)
      al.Add(23)
      al.Add(9)
      Console.WriteLine("Capacity: {0} ", al.Capacity)
      Console.WriteLine("Count: {0}", al.Count)
      Console.Write("Content: ")
      For Each i In al
          Console.Write("{0} ", i)
      Next i
      Console.WriteLine()
      Console.Write("Sorted Content: ")
      al.Sort()
      For Each i In al
          Console.Write("{0} ", i)
      Next i
      Console.WriteLine()
      Console.ReadKey()
   End Sub
End Module

当上述代码被编译和执行时,它产生以下结果:
Adding some numbers:
Capacity: 8
Count: 7
Content: 45 78 33 56 12 23 9
Content: 9 12 23 33 45 56 78   

以上内容是否对您有帮助:
二维码
建议反馈
二维码