我可以得到一個如何在Visual Basic .NET中製作Vector或ArrayList的例子嗎?Visual Basic中的Arraylist .net
2
A
回答
11
3
請嘗試以下
Dim list As New ArrayList()
list.Add("hello")
list.Add("world")
For Each cur As String in list
Console.WriteLine(cur)
Next
1
Module Module1
Sub Main()
Dim al As New ArrayList()
al.Add("1")
al.Add("2")
al.Add("3")
End Sub
End Module
1
如果您碰巧使用VB10,應該可以使用以下語法。
Dim list As New List(Of Integer) From { 1, 2, 3, 4, 5 }
0
您可以使用此:
Dim a As New ArrayList()
a.Add("Item1")
a.Add("Item2")
a.Add("Item3")
1
添加值
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
輸出
的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
輸出
計數,清除
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
輸出
添加,刪除元素
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
輸出
皮爾斯
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
輸出
男人 女人 廠
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
輸出
鳥 工廠
相關問題
- 1. 在Visual Basic .NET中的AddressOf
- 2. Visual Basic .NET:計劃
- 3. Visual Basic .NET邏輯
- 4. 的Visual Basic .NET編碼
- 5. Arraylist在Visual Studio中.net 2005
- 6. Visual Basic .net中圖像的透明度?
- 7. .NET Framework 4.0中的Visual Basic 6.0
- 8. Visual Basic .NET調度線程
- 9. Visual Basic .NET Power Pack 2003
- 10. Visual Basic .Net 3D引擎
- 11. CINT與Math.Round在Visual Basic .NET
- 12. Visual Basic .Net對象和類
- 13. Visual Basic .Net語音命令
- 14. Visual basic .net 2008部署
- 15. Visual Basic .NET中:如何從參數
- 16. 在Visual Basic .NET中提取Zip文件
- 17. 是否有Visual Basic .NET的機械化?
- 18. 的Visual Basic .NET反序列化
- 19. Visual Studio 2010中的內存不足問題Visual Basic .NET 4.0
- 20. 複製Excel中排在Visual Basic .NET(VS 2008,.NET Framework 3.5中)
- 21. Basic Java arraylist
- 22. powerpoints中的visual basic
- 23. Visual Basic中的OLEObjects
- 24. Linux中的Visual Basic
- 25. 對Visual Basic中
- 26. 從Visual Basic調用.Net類6
- 27. Visual Basic .Net應用程序部署
- 28. 從Visual Basic調用.Net類6
- 29. Visual Basic - 調用.net運行時
- 30. Visual Basic .Net - 查詢數據庫
+1(提到List(Of T)) - 這是比ArrayList更好的選擇。 – 2009-09-21 17:06:22
ArrayList將被刪除,並且據我所知,在某些平臺(如Silverlight)中甚至不可用。 – 2009-09-21 20:46:02