What is the best way to clear an array of strings?
清除字符串数组的最好方法是什么?
5 个解决方案
#1
21
Wrong way:
错误的方式:
myArray = Nothing
Only sets the variable pointing to the array to nothing, but doesn't actually clear the array. Any other variables pointing to the same array will still hold the value. Therefore it is necessary to clear out the array.
只将指向数组的变量设置为空,而不清除数组。指向相同数组的任何其他变量仍然保留该值。因此,必须清除该数组。
Correct Way
正确的方法
Array.Clear(myArray,0,myArray.Length)
#2
6
And of course there's the VB way using the Erase keyword:
当然还有VB的方法使用的是删除关键字:
Dim arr() as String = {"a","b","c"}
Erase arr
#3
2
Depending what you want:
根据你想要的东西:
- Assign Nothing (null)
- 分配什么(空)
- Assign a new (empty) array
- 分配一个新的(空的)数组
- Array.Clear
- Array.Clear
Last is likely to be slowest, but only option if you don't want a new array.
最后一个可能是最慢的,但是只有当您不想要一个新的数组时才可以选择。
#4
2
If you're needing to do things like clear, you probably want a collection like List(Of String)
rather than an array.
如果您需要执行clear之类的操作,您可能需要一个类似List(Of String)的集合,而不是一个数组。
#5
1
redim arr(1,1,1,1) and then redim (z,x,y,v) to your dimensions
重拨arr(1,1,1,1)然后重拨(z,x,y,v)到你的维度
#1
21
Wrong way:
错误的方式:
myArray = Nothing
Only sets the variable pointing to the array to nothing, but doesn't actually clear the array. Any other variables pointing to the same array will still hold the value. Therefore it is necessary to clear out the array.
只将指向数组的变量设置为空,而不清除数组。指向相同数组的任何其他变量仍然保留该值。因此,必须清除该数组。
Correct Way
正确的方法
Array.Clear(myArray,0,myArray.Length)
#2
6
And of course there's the VB way using the Erase keyword:
当然还有VB的方法使用的是删除关键字:
Dim arr() as String = {"a","b","c"}
Erase arr
#3
2
Depending what you want:
根据你想要的东西:
- Assign Nothing (null)
- 分配什么(空)
- Assign a new (empty) array
- 分配一个新的(空的)数组
- Array.Clear
- Array.Clear
Last is likely to be slowest, but only option if you don't want a new array.
最后一个可能是最慢的,但是只有当您不想要一个新的数组时才可以选择。
#4
2
If you're needing to do things like clear, you probably want a collection like List(Of String)
rather than an array.
如果您需要执行clear之类的操作,您可能需要一个类似List(Of String)的集合,而不是一个数组。
#5
1
redim arr(1,1,1,1) and then redim (z,x,y,v) to your dimensions
重拨arr(1,1,1,1)然后重拨(z,x,y,v)到你的维度