简体   繁体   English

如何将字符串添加到数组 VB.NET?

[英]How to add a string to an array VB.NET?

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.数据库项目将始终是一个字符串。

Arrays have a fixed length. Arrays 具有固定长度。 Use a List(Of String) instead:使用List(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 ).注意:列表在内部使用 arrays 并自动调整它们的大小(基本上与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.他们不是在每次添加时将列表大小增加一个元素,而是从 4 的数组大小开始,并且每次数组变得太小时将其大小加倍。 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.这减少了所需的复制操作次数,因为增加数组的大小意味着创建一个新数组并将旧数组的内容复制到新数组。

So, there is really no point in using Redim yourself, as lists make it automatically and efficiently for you.因此,您自己使用Redim真的毫无意义,因为列表会自动高效地为您服务。


By the way InStr(...) - 1 <> -1 is a strange condition.顺便说一句, InStr(...) - 1 <> -1是一个奇怪的条件。 What is its purpose?它的目的是什么? InStr(...) <> 0 is equivalent. InStr(...) <> 0是等效的。 Shouldn't the condition be InStr(...) <> -1 ?条件不应该是InStr(...) <> -1吗? Or membertable.Rows(i)("name").Contains(txtSearch.Text) ?或者membertable.Rows(i)("name").Contains(txtSearch.Text)

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.重要的是使用 PRESERVE 关键字。 Without that, your array will be cleared.否则,您的阵列将被清除。

The better way would be to not use an array at all but to use a collection or list.更好的方法是根本不使用数组,而是使用集合或列表。

Use a List(Of String) instead of an array.使用List(Of String)而不是数组。 Also you could LINQ the results.你也可以LINQ结果。 It is also better not to name a variable the same name as a Data Type.最好不要将变量命名为与数据类型同名。

Dim myList = (From dr As DataRow In membertable.Rows Where dr("name").ToString = txtSearch.Text).ToList

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 List s.当它多次发生时,你不应该使用任何形式的 arrays 包括List s。 Maybe LinkedList s are what you're searching for.也许LinkedList是您正在寻找的。 They provide efficient adding (especially anywhere, further deleting is efficient too).它们提供高效的添加(尤其是在任何地方,进一步删除也很有效)。 Their only disadvantage is "slow" O(n) indexing.它们唯一的缺点是“慢”的 O(n) 索引。 Sequential For Each lookup is always O(n) and there is hardly any difference between them and arrays. Sequential For Each查找总是 O(n),它们与 arrays 之间几乎没有任何区别。

And if you just create the elements and then process them, you can use Iterator Function s (also possible as lambda inside your procedure).如果您只是创建元素然后处理它们,则可以使用Iterator Function s(也可以在您的过程中使用 lambda)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM