vb.net - 每当我尝试输出数组时,数组都会继续输出“System.Int32 []” - 有人知道为什么吗?
问题描述
For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
For valueWithinArray As Integer = 0 To arrayToSort.Length - 2
If arrayToSort(arrayValue) > arrayToSort(arrayValue + 1) Then
Dim tempStorage As Integer = arrayToSort(arrayValue)
arrayToSort(arrayValue) = arrayToSort(arrayValue + 1)
arrayToSort(arrayValue + 1) = tempStorage
End If
Next
Next
该数组被声明为arrayToSort(7)
,是从 9 到 2 的整数(包括 9 到 2)。
解决方案
我将您的变量名称更改valueWithinArray
为index
. 它实际上不是数组中的值,而是元素的索引。arrayValue
您在您真正想要的地方引入了另一个未声明的变量 ( ) valueWithinArray
(现在称为index
)。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayToSort() As Integer = {6, 8, 7, 2, 9, 4, 5, 3}
For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
For index As Integer = 0 To arrayToSort.Length - 2
If arrayToSort(index) > arrayToSort(index + 1) Then
Dim tempStorage As Integer = arrayToSort(index)
arrayToSort(index) = arrayToSort(index + 1)
arrayToSort(index + 1) = tempStorage
End If
Next
Next
For Each i In arrayToSort
Debug.Print(i.ToString)
Next
End Sub
这是一个很好的学习练习,但为了节省一些打字......
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim arrayToSort() As Integer = {9, 8, 7, 6, 5, 4, 3, 2}
Array.Sort(arrayToSort)
For Each i In arrayToSort
Debug.Print(i.ToString)
Next
End Sub
结果将显示在即时窗口中。
推荐阅读
- python - 从具有大于 5 个缺失值的行中删除缺失值,然后打印每列中缺失值的百分比
- c++ - 新运算符 C++17 中的对齐 - Visual Studio
- docker - CoreOS - 将主机端口暴露给 VPN
- java - 断言抛出多个异常
- c# - 如何在 C# 中使用 Quartz.net 安排作业(方法)?
- haskell - 在最后一次递归之前忽略类型声明
- javascript - 在 mousedown 上将变量设置为负值
- macos - 使用 Mac 在 Chrome 操作系统设备上调试应用程序
- sql - 获得每所学校学生的最高分
- vhdl - 在 Basys3 FPGA 上使用 4x4 键盘