在Visual Basic中打印(多)维数组 [英] Print (multi)dimensional array in Visual Basic

查看:194
本文介绍了在Visual Basic中打印(多)维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种简单的方法来打印一个数组,这可能是多维的,到控制台在VB.NET用于调试的目的(即只检查该数组的内容是正确的)。

这是一个Objective-C的背景的NSLog 函数打印一个相当不错格式化输出,如一个一维数组下面来:

  myArray的{
    0 => 你好
    1 => 世界
    2 => 美好的一天
    3 => 给你!
}

和为多维阵列(下面是一个二维阵列的输出的一个例子)相似:

  myTwoDArray {
    0 => {
        0 => 元件
        1 => 零
    }
    1 => {
        0 => 元件
        1 => 一
    }
    2 => {
        0 => 元件
        1 => 二
    }
    3 => {
        0 => 元件
        1 => 三
    }
}


解决方案

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

 公共共享子函数printValues​​(myArr,该作为阵列)
  昏暗参考译文]字符串=
  昏暗myEnumerator作为System.Collections.IEnumerator = myArr.GetEnumerator()
  昏暗我为整数= 0
  昏暗COLS为整数= myArr.GetLength(myArr.Rank - 1)
  虽然myEnumerator.MoveNext()
    如果I<然后COLS
      I + = 1
    其他
      Console.WriteLine()
      S = S&安培; vbCrLf
      I = 1
    万一
    Console.Write(ControlChars.Tab +{0},myEnumerator.Current)
    S = S&安培; myEnumerator.Current&安培;
  虽然结束
  Console.WriteLine()
  MSGBOX(S)
结束小组

有关在非控制台应用程序测试出功能的缘故,我已经添加字符串变量S,你应该能够当你在一个控制台应用程序使用该函数忽略。

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).

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

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"
    }
}

解决方案

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.

这篇关于在Visual Basic中打印(多)维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆