vb.net Visual Basic .net 中的数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1455562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:26:21  来源:igfitidea点击:

Arraylist in Visual Basic .net

vb.netvectorarraylist

提问by crazybmanp

Can I get an example of how to make something like a Vector or an ArrayList in Visual Basic .NET?

我可以获得如何在 Visual Basic .NET 中制作 Vector 或 ArrayList 之类的示例吗?

回答by cdmckay

Dim list As New ArrayList

or (equivalently):

或(相当于):

Dim list As ArrayList = New ArrayList

If you want a generic List (very similiar to ArrayList):

如果你想要一个通用列表(非常类似于 ArrayList):

Dim list As New List(Of String)

Also see the ArrayListand Listdocumentation.

另请参阅ArrayListList文档。

回答by JaredPar

Try the following

尝试以下

Dim list As New ArrayList()
list.Add("hello")
list.Add("world")
For Each cur As String in list
  Console.WriteLine(cur)
Next

回答by Chris McCall

Module Module1

    Sub Main()
        Dim al As New ArrayList()
        al.Add("1")
        al.Add("2")
        al.Add("3")
    End Sub

End Module

回答by Brian Gideon

If you happen to be using VB10 you should be able to use the following syntax.

如果您碰巧使用 VB10,您应该能够使用以下语法。

Dim list As New List(Of Integer) From { 1, 2, 3, 4, 5 }

回答by user2029909

Add values

添加值

Dim list As New ArrayList
list.Add("One")
list.Add("Two")
list.Add("Three")

Parameter example

参数示例

Module Module1

    Sub Main()
    ' Create an ArrayList and add two elements to it.
    Dim list As New ArrayList
    list.Add(5)
    list.Add(7)
    ' Use ArrayList as an argument to the method.
    Example(list)
    End Sub

    ''' <summary>
    ''' Receives ArrayList as argument.
    ''' </summary>
    Private Sub Example(ByVal list As ArrayList)
    Dim num As Integer
    For Each num In list
        Console.WriteLine(num)
    Next
    End Sub

End Module

Output

输出

5 7

5 7

AddRange

添加范围

Module Module1

    Sub Main()
    ' Create an ArrayList and add two elements.
    Dim list1 As New ArrayList
    list1.Add(5)
    list1.Add(7)
    ' Create a separate ArrayList.
    Dim list2 As New ArrayList
    list2.Add(10)
    list2.Add(13)
    ' Add this ArrayList to the other one.
    list1.AddRange(list2)
    ' Loop over the elements.
    Dim num As Integer
    For Each num In list1
        Console.WriteLine(num)
    Next
    End Sub

End Module

Output

输出

5 7 10 13

5 7 10 13

Count, Clear

计数,清除

Module Module1

    Sub Main()
    ' Add two elements to the ArrayList.
    Dim list As New ArrayList
    list.Add(9)
    list.Add(10)
    ' Write the Count.
    Console.WriteLine(list.Count)
    ' Clear the ArrayList.
    list.Clear()
    ' Write the Count again.
    Console.WriteLine(list.Count)
    End Sub

End Module

Output

输出

2 0

2 0

Add, remove elements

添加、删除元素

Module Module1

    Sub Main()
    ' Create an ArrayList and add three strings to it.
    Dim list As New ArrayList
    list.Add("Dot")
    list.Add("Net")
    list.Add("Perls")
    ' Remove a string.
    list.RemoveAt(1)
    ' Insert a string.
    list.Insert(0, "Carrot")
    ' Remove a range.
    list.RemoveRange(0, 2)
    ' Display.
    Dim str As String
    For Each str In list
        Console.WriteLine(str)
    Next
    End Sub

End Module

Output

输出

Perls

珀尔斯

TryCast

试播

Module Module1

    Sub Main()
    ' Create a new ArrayList.
    Dim list As New ArrayList
    list.Add("man")
    list.Add("woman")
    list.Add("plant")
    ' Loop over the ArrayList with a For loop.
    Dim i As Integer
    For i = 0 To list.Count - 1
        ' Cast to a string.
        Dim str As String = TryCast(list.Item(i), String)
        Console.WriteLine(str)
    Next i
    End Sub

End Module

Output

输出

man woman plant

男人女人植物

GetRange

获取范围

Module Module1

    Sub Main()
    ' Create new ArrayList.
    Dim list1 As New ArrayList
    list1.Add("fish")
    list1.Add("amphibian")
    list1.Add("bird")
    list1.Add("plant")
    ' Create a new ArrayList and fill it with the range from the first one.
    Dim list2 As New ArrayList
    list2 = list1.GetRange(2, 2)
    ' Loop over the elements.
    Dim str As String
    For Each str In list2
        Console.WriteLine(str)
    Next
    End Sub

End Module

Output

输出

bird plant

鸟类植物

回答by Saurabh Sharma

You can use this:

你可以使用这个:

Dim a As New ArrayList()
a.Add("Item1")
a.Add("Item2")
a.Add("Item3")