Могу ли я получить пример того, как сделать что-то вроде Vector или ArrayList в Visual Basic .NET?
Список массивов в Visual Basic .net
Ответы (5)
Dim list As New ArrayList
или (эквивалентно):
Dim list As ArrayList = New ArrayList
Если вам нужен общий список (очень похожий на ArrayList):
Dim list As New List(Of String)
См. также ArrayList и Список документации.
person
cdmckay
schedule
21.09.2009
+1 за упоминание List(Of T) - это лучшая альтернатива ArrayList.
- person Reed Copsey; 21.09.2009
ArrayList будет устаревшим и, насколько я понимаю, даже недоступен на некоторых платформах, таких как Silverlight.
- person Jonathan Allen; 22.09.2009
Попробуйте следующее
Dim list As New ArrayList()
list.Add("hello")
list.Add("world")
For Each cur As String in list
Console.WriteLine(cur)
Next
person
JaredPar
schedule
21.09.2009
Если вы используете VB10, вы сможете использовать следующий синтаксис.
Dim list As New List(Of Integer) From { 1, 2, 3, 4, 5 }
person
Brian Gideon
schedule
21.09.2009
Добавить значения
Dim list As New ArrayList
list.Add("One")
list.Add("Two")
list.Add("Three")
Пример параметра
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
Выход
5 7
Добавить диапазон
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
Выход
5 7 10 13
Считать, очищать
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
Выход
2 0
Добавить, удалить элементы
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
Выход
Перлз
Попробуйте
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
Выход
мужчина женщина растение
Получить диапазон
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
Выход
птица растение
person
user2029909
schedule
31.01.2013
Вы можете использовать это:
Dim a As New ArrayList()
a.Add("Item1")
a.Add("Item2")
a.Add("Item3")
person
Saurabh Sharma
schedule
30.09.2012