如何将字符串添加到数组 VB.NET?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25792892/
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
How to add a string to an array VB.NET?
提问by OmnivorousOctopus
I have an array like so
我有一个像这样的数组
Dim array() As String = {}
and the following code
和以下代码
For i = 0 To membertable.Rows.Count - 1
If InStr(membertable.Rows(i)("name"), txtSearch.Text, CompareMethod.Text) - 1 _
<> -1 And txtSearch.Text.Length >= 3 Then
found = True
'add the item that matches the criteria to the array here.
End If
Next i
So the code loops through the rows of an access table and every time it finds a value under the "name" column that matches the criteria I want to add that item to the array. the database item will always be a string.
因此,代码循环访问访问表的行,每次在“名称”列下找到与我想将该项目添加到数组的条件相匹配的值时。数据库项将始终是一个字符串。
回答by Olivier Jacot-Descombes
Arrays have a fixed length. Use a List(Of String)instead:
数组具有固定长度。使用 aList(Of String)代替:
Dim list As New List(Of String)()
...
list.Add(someString);
Note: Lists use arrays internally and resize them automatically (basically doing the same as Redim Preserve). Instead of increasing the list size by one element at each addition, they start with an array size of 4 and double its size each time the array gets too small. This reduces the number of copy operations needed, since increasing the size of an array means to create a new array and to copy the contents of the old one to the new one.
注意:列表在内部使用数组并自动调整它们的大小(基本上与 相同Redim Preserve)。不是在每次添加时将列表大小增加一个元素,而是从数组大小 4 开始,每次数组变得太小时将其大小加倍。这减少了所需的复制操作数量,因为增加数组的大小意味着创建一个新数组并将旧数组的内容复制到新数组。
So, there is really no point in using Redimyourself, as lists make it automatically and efficiently for you.
所以,使用Redim你自己真的没有意义,因为列表会让你自动有效地使用它。
By the way InStr(...) - 1 <> -1is a strange condition. What is its purpose? InStr(...) <> 0is equivalent. Shouldn't the condition be InStr(...) <> -1? Or membertable.Rows(i)("name").Contains(txtSearch.Text)?
顺便说一句InStr(...) - 1 <> -1是一个奇怪的条件。它的目的是什么?InStr(...) <> 0是等价的。条件不应该是InStr(...) <> -1?或者membertable.Rows(i)("name").Contains(txtSearch.Text)?
回答by Steve
To answer your question, you need to re-dimension you array each time you want to add another item:
要回答您的问题,每次要添加另一个项目时,您都需要重新调整数组的尺寸:
Redim Preserve array(array.length)
Then add your item to the last one:
然后将您的项目添加到最后一个:
array(array.length - 1) = ???
The important thing is using the PRESERVE keyword. Without that, your array will be cleared.
重要的是使用 PRESERVE 关键字。否则,您的阵列将被清除。
The better way would be to not use an array at all but to use a collection or list.
更好的方法是根本不使用数组,而是使用集合或列表。
回答by OneFineDay
Use a List(Of String)instead of an array. Also you could LINQthe results. It is also better not to name a variable the same name as a Data Type.
使用 aList(Of String)而不是数组。你也可以LINQ得到结果。最好不要将变量命名为与数据类型相同的名称。
Dim myList = (From dr As DataRow In membertable.Rows Where dr("name").ToString = txtSearch.Text).ToList
回答by Bolpat
It depends on how often you add elements to your array. When it happens many times, you should not use any form of arrays including Lists. Maybe LinkedLists are what you're searching for. They provide efficient adding (especially anywhere, further deleting is efficient too). Their only disadvantage is "slow" O(n) indexing. Sequential For Eachlookup is always O(n) and there is hardly any difference between them and arrays.
这取决于您向数组添加元素的频率。当它多次发生时,你不应该使用任何形式的数组,包括Lists。也许LinkedLists 就是您要搜索的内容。它们提供了高效的添加(尤其是在任何地方,进一步删除也是有效的)。他们唯一的缺点是“缓慢”的 O(n) 索引。顺序For Each查找总是 O(n) 并且它们和数组之间几乎没有任何区别。
And if you just create the elements and then process them, you can use Iterator Functions (also possible as lambda inside your procedure).
如果您只是创建元素然后处理它们,则可以使用Iterator Functions(也可以在您的过程中用作 lambda)。

