vb.net 在 Visual Basic 中打印(多)维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15403361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:47:22  来源:igfitidea点击:

Print (multi)dimensional array in Visual Basic

arraysvb.net

提问by Skoota

Is there an easy way to print an array, which is potentially multi-dimensional, to the Console in VB.NET for the purpose of debugging (i.e. just checking that the contents of the array is correct).

是否有一种简单的方法可以将可能是多维的数组打印到 VB.NET 中的控制台以进行调试(即仅检查数组的内容是否正确)。

Coming from an Objective-C background the NSLogfunction prints a reasonably well formatted output, such as the following for a one-dimensional array:

来自 Objective-C 背景的NSLog函数打印了一个格式合理的输出,例如一维数组的以下输出:

myArray {
    0 => "Hello"
    1 => "World"
    2 => "Good Day"
    3 => "To You!"
}

and similar for a multi-dimensional array (the following is an example of a two-dimensional array output):

与多维数组类似(以下是二维数组输出的示例):

myTwoDArray {
    0 => {
        0 => "Element"
        1 => "Zero"
    }
    1 => {
        0 => "Element"
        1 => "One"
    }
    2 => {
        0 => "Element"
        1 => "Two"
    }
    3 => {
        0 => "Element"
        1 => "Three"
    }
}

回答by Zeddy

I don't think there is any native (in-built) function to do that, Yet the function below should work fine.

我认为没有任何本机(内置)函数可以做到这一点,但下面的函数应该可以正常工作。

Public Shared Sub PrintValues(myArr As Array)
  Dim s As String = ""
  Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
  Dim i As Integer = 0
  Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
  While myEnumerator.MoveNext()
    If i < cols Then
      i += 1
    Else
      'Console.WriteLine()
      s = s & vbCrLf
      i = 1
    End If
    'Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
    s = s & myEnumerator.Current & " "
  End While
  'Console.WriteLine()
  MsgBox(s)
End Sub

For the sake of testing the function out in a non console application, I have added the string variable S, which you should be able to omit when you use the function in a console application.

为了在非控制台应用程序中测试该函数,我添加了字符串变量 S,当您在控制台应用程序中使用该函数时,您应该可以省略该变量。