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.
因此,代码循环遍历访问表的行,并且每次在“name”列下找到与我要将该项添加到数组的条件匹配的值。数据库项始终是一个字符串。
4 个解决方案
#1
5
Arrays have a fixed length. 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
). 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 Redim
yourself, as lists make it automatically and efficiently for you.
因此,自己使用Redim毫无意义,因为列表可以自动高效地为您提供。
By the way InStr(...) - 1 <> -1
is a strange condition. What is its purpose? InStr(...) <> 0
is 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”)。包含(txtSearch.Text)?
#2
2
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.
更好的方法是根本不使用数组,而是使用集合或列表。
#3
1
Use a List(Of String)
instead of an array. Also you could LINQ
the results. It is also better not to name a variable the same name as a Data Type.
使用List(Of String)而不是数组。你也可以LINQ结果。最好不要将变量命名为与数据类型相同的名称。
Dim myList = (From dr As DataRow In membertable.Rows Where dr("name").ToString = txtSearch.Text).ToList
#4
0
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. Maybe LinkedList
s 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 Each
lookup is always O(n) and there is hardly any difference between them and arrays.
这取决于您向阵列添加元素的频率。当它多次发生时,你不应该使用任何形式的数组,包括列表。也许LinkedLists是你正在寻找的。它们提供有效的添加(特别是在任何地方,进一步删除也很有效)。他们唯一的缺点是“慢”O(n)索引。顺序对于每个查找总是O(n)并且它们与数组之间几乎没有任何差异。
And if you just create the elements and then process them, you can use Iterator Function
s (also possible as lambda inside your procedure).
如果您只是创建元素然后处理它们,您可以使用迭代器函数(也可以作为程序中的lambda)。
#1
5
Arrays have a fixed length. 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
). 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 Redim
yourself, as lists make it automatically and efficiently for you.
因此,自己使用Redim毫无意义,因为列表可以自动高效地为您提供。
By the way InStr(...) - 1 <> -1
is a strange condition. What is its purpose? InStr(...) <> 0
is 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”)。包含(txtSearch.Text)?
#2
2
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.
更好的方法是根本不使用数组,而是使用集合或列表。
#3
1
Use a List(Of String)
instead of an array. Also you could LINQ
the results. It is also better not to name a variable the same name as a Data Type.
使用List(Of String)而不是数组。你也可以LINQ结果。最好不要将变量命名为与数据类型相同的名称。
Dim myList = (From dr As DataRow In membertable.Rows Where dr("name").ToString = txtSearch.Text).ToList
#4
0
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. Maybe LinkedList
s 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 Each
lookup is always O(n) and there is hardly any difference between them and arrays.
这取决于您向阵列添加元素的频率。当它多次发生时,你不应该使用任何形式的数组,包括列表。也许LinkedLists是你正在寻找的。它们提供有效的添加(特别是在任何地方,进一步删除也很有效)。他们唯一的缺点是“慢”O(n)索引。顺序对于每个查找总是O(n)并且它们与数组之间几乎没有任何差异。
And if you just create the elements and then process them, you can use Iterator Function
s (also possible as lambda inside your procedure).
如果您只是创建元素然后处理它们,您可以使用迭代器函数(也可以作为程序中的lambda)。