首页 > 解决方案 > 每当我尝试输出数组时,数组都会继续输出“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)。

标签: vb.net

解决方案


我将您的变量名称更改valueWithinArrayindex. 它实际上不是数组中的值,而是元素的索引。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

结果将显示在即时窗口中。


推荐阅读