在 VB.NET 中在运行时调整数组大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/967423/
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
Resizing an array at runtime in VB.NET
提问by l--''''''---------''''''''''''
In my Windows Forms application at runtime I will be resizing an array each time I add an element. So first I must resize to the size + 1
, and then add a member to this index. How do I do this?
在运行时的 Windows 窗体应用程序中,每次添加元素时,我都会调整数组的大小。所以首先我必须调整到size + 1
,然后向这个索引添加一个成员。我该怎么做呢?
回答by Joel Coehoorn
You coulduse the ReDim
statement, but this really isn't your best option. If your array will be changing sizes often, especiallyas it sounds like you're just appending, you should probably use a generic List(Of T)
or similar collection type.
您可以使用该ReDim
语句,但这确实不是您的最佳选择。如果您的数组经常改变大小,尤其是听起来您只是在追加,您可能应该使用通用List(Of T)
或类似的集合类型。
You can use it just like you use an array, with the addition that adding an item to the end is as easy as MyList.Add(item)
您可以像使用数组一样使用它,另外在末尾添加一个项目就像使用一样简单 MyList.Add(item)
To use a generic list, add Imports System.Collections.Generics
to the top of the file. Then, you would declare a new integer list like this:
要使用通用列表,请添加Imports System.Collections.Generics
到文件顶部。然后,您将声明一个新的整数列表,如下所示:
Dim MyList As New List(Of Integer)()
or a string list like this:
或这样的字符串列表:
Dim MyList As New List(Of String)()
You should get the idea.
你应该明白了。
回答by Jules
The suggested ReDim's need the Preserve keyword for this scenario.
对于这种情况,建议的 ReDim 需要 Preserve 关键字。
ReDim Preserve MyArray(n)
回答by Corniel Nobel
Using a generic list is (as suggested) the best idea. If you however want to change the size of an Array, you can use Array.Resize(ByRef arr, newSize)
.
使用通用列表是(如建议的)最好的主意。但是,如果您想更改数组的大小,则可以使用Array.Resize(ByRef arr, newSize)
.
ReDim is not a good (pretty bad) idea (VB specific legacy, extremely slow).
ReDim 不是一个好(很糟糕)的主意(VB 特定的遗留问题,非常慢)。
回答by tekBlues
I would prefer some type of collection class, but if you WANT to use an array do it like this:
我更喜欢某种类型的集合类,但如果您想使用数组,请这样做:
Dim arr() As Integer
Dim cnt As Integer = 0
Dim ix As Integer
For ix = 1 To 1000
cnt = cnt + 1
ReDim arr(cnt)
arr(cnt - 1) = ix
Next
回答by Gaurang
You can also make your own collection class. A good programming exercise for new programmers.
您也可以制作自己的收藏类。对于新程序员来说是一个很好的编程练习。
Public Class MyList
Private Items() As String
Private No As Integer = 0
Public Sub Add(ByVal NewItem As String)
''Create a temporary new string array
Dim CopyString(No) As String
''Copy values from Global Variable Items() to new CopyString array
For i As Integer = 0 To No - 1
CopyString(i) = Items(i)
Next
''Add new value - NewItem - to CopyString
CopyString(No) = NewItem
''Increment No to No + 1
No += 1
''Copy CopyString to Items
Items = CopyString
'Discard CopyString
CopyString = Nothing
End Sub
Public Sub Show(ByVal index As Integer)
MsgBox(Items(index))
End Sub
End Class
''Now create a form with a TextBox name - txt, Button1 and Button2
Public Class Form1
''Declare txts as a new MyList Class
Private txts As New MyList
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''Add text to txts which is a MyList Class
txts.Add(txt.Text)
txt.Text = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
''Display value at a specific index
txts.Show(Convert.ToInt16(txt.Text))
txt.Text = ""
End Sub
End Class
回答by Dillie-O
Use the ReDim command to specify the new size.
使用 ReDim 命令指定新大小。
ReDim MyArray(MyArray.Length + 1)
回答by dsas
As Joel says, use a list.
正如乔尔所说,使用列表。
Dim MyList As New List(Of String)
Don't forget to change Of String to be Of whichever datatype you're using.
不要忘记将字符串更改为您正在使用的任何数据类型。
回答by Juver Paredes
This work for me
这对我有用
Dim Table1 As New DataTable
' Define columns
Table1.Columns.Add("Column1", GetType(System.String))
Table1.Columns.Add("Column2", GetType(System.Int32))
Table1.Columns.Add("Column3", GetType(System.Int32))
' Add a row of data
Table1.Rows.Add("Item1", 44, 99)
Table1.Rows.Add("Item2", 42, 3)
Table1.Rows.Add("Item3", 42, 3)
Table1.Rows.Add("Item4", 42, 3)
Dim arr(-1) As String
For Each dr As DataRow In Table1.Rows
ReDim Preserve arr(arr.Length)
arr(arr.Length - 1) = dr("Column1")
Next