Visual Basic.net ArrayList 用法

ArrayList代表一个动态的,此类实现大小可动态调整的对象数组,提供了一般的集合功能,适合于大多数用户应用。

ArrayList有三个构造器:

1ArrayList()

  直接实例化,后调用方法添加或者删除列表条目

Dim pan1 as New ArrayList()

pan1.add(“number1”)

dim arr1 as New Collection

arr1.add(“number2”)

arr1.add(“ss3”)

pan1.addrange(arr1)

2ArrayList(int32)

ArrayList的构造器传递一个整数,以设定 ArrayList的初始容量。
Dim pan1 as ArrayList(5) ‘设定ArrayList初始容量

Dim cc as Collection

cc.add(string1)

cc.add(string2)

pan1.addrange(cc)

pan1.capacity=8    ‘将arraylist容量重新设定为8

MsgBox(pan1.capacity)

‘TrimToSize()方法将arraylist容量压缩成实际的元素数量

‘pan1.capacity 与pan1.trimtosize()及pan1.count等价

pan1.trimtosize()

3.ArrayList(ICollection)

ArrayList的构造器可以接受一个集合

Dim cc As New Collection
cc.add("string1")
cc.Add("string2")
Dim pan1 As New ArrayList(cc)

实例:

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    

    Public Shared Sub Main()

        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("Hello")
        myAL.Add("World")
        myAL.Add("!")

        ' Displays the properties and values of the ArrayList.
        Console.WriteLine("myAL")
        Console.WriteLine("    Count:    {0}", myAL.Count)
        Console.WriteLine("    Capacity: {0}", myAL.Capacity)
        Console.Write("    Values:")
        PrintValues(myAL)
    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class




你可能感兴趣的:(basic,实例,ArrayList,Visual)