我声明了一个字符串数组,并将每个元素初始化为“5”。但是当我在For-Each之后用断点调试时,我的所有数组成员都没有Nothing。我在Visual Studio 2010上。请帮忙吗?
Dim words(999) As String
For Each word As String In words
word = "5"
Next
答案 0 :(得分:3)
你所拥有的东西对阅读数组有好处,但不适合写它们。您的代码转换为:
Create words array with 1000 elements
For each index in the array
word = words(index)
word = "5"
Next index
它决不会将这个词放回数组中。代码丢失了:
...
words(index) = word
Next index
您需要做的是:
Dim words(999) As String
For index As Integer = 0 to words.Length - 1
words(index) = "5"
Next
修改:回复以下评论。
初始化数组后,可以使用For / Each循环读取元素。
For Each word As String in words
Console.WriteLine(word)
Next
这与:
相同For index As Integer = 0 To words.Length - 1
Console.WriteLine(words(index))
Next
答案 1 :(得分:1)
你也可以这样做:
Dim Words() As String = Enumerable.Repeat("5", 1000).ToArray()