需要一个可以创建值为0-50的数组的程序,然后将它们输出到列表中。它似乎只输出'int 32'。什么错误的想法?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Numbers(50) As Integer
Dim Index As Integer
Do Until Numbers(Index) <= 50
Index = Index + 1
Numbers(Index) = Index
Loop
lstnumbers.Items.Add(Numbers)
End Sub
End Class
答案 0 :(得分:1)
一些小改动...... Do Until&lt;会给出一堆零 还移动了一些东西以避免Index超出范围并在那里得到零。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Numbers(50) As Integer
Dim Index As Integer
Do Until Index >= 51
Numbers(Index) = Index
lstNumbers.Items.Add(Numbers(Index))
Index = Index + 1
Loop
答案 1 :(得分:0)
这里有两个选项:
<强>一:强>
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lstnumbers As New ArrayList()
Dim Numbers(50) As Integer
Dim Index As Integer
Do Until Numbers(Index) <= 50
Index = Index + 1
Numbers(Index) = Index
Loop
lstnumbers.AddRange(Numbers)
End Sub
End Class
<强>两个强>
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Numbers(50) As Integer
Dim Index As Integer
Do Until Numbers(Index) <= 50
Index = Index + 1
Numbers(Index) = Index
lstnumbers.Items.Add(Index)
Loop
End Sub
End Class
这是因为Items.Add
每次只收一件商品,这意味着Items.Add(Numbers)
只会添加int 32
。
ArrayList AddRange
会占用一个队列并添加它的范围,但是如果你知道你的ArrayList
确切包含多少元素,则不建议使用它,因为它&#39;大小是动态变化的。
在这个例子中没有区别,因为你已经使用了一个列表。