【问题标题】:How to create multi-dimensional jagged arrays in VbScript?如何在 VbScript 中创建多维锯齿状数组?
【发布时间】:2010-05-19 08:24:49
【问题描述】:

我需要创建多维字符串数组。数组的每一行可以有不同数量的字符串。类似于以下代码:

twoDimension = Array(Array())  
ReDim Preserve twoDimension(3)  
For i = 0 to 2  
 If i = 1 Then  
  twoDimension(i) = Array(1,2,3)  
 End If  
 If i = 2Then  
     twoDimension(i) = Array(1,2,3,4,5)  
   End If  
Next  

【问题讨论】:

    标签: vbscript jagged-arrays dynamic-arrays


    【解决方案1】:

    字典怎么样

    Set a = CreateObject("Scripting.Dictionary")
    a.Add 0, Array(1,2,3)
    a.Add 1, Array(4,5,6)
    MsgBox a.Count
    MsgBox a.Item(0)(2)
    MsgBox a.Item(1)(1)
    

    【讨论】:

      【解决方案2】:

      在 VBScript 中使用锯齿状数组并没有错。您的代码存在一些小问题(ReDim 到 3,但仅将值分配给 2,不必要地使用 For 循环来分配值),但总的来说,这是正确的语法。

      Option Explicit
      
      Dim twoDimension, i, j
      
      twoDimension = Array(Array())  
      ReDim Preserve twoDimension(2)
      
      twoDimension(1) = Array(1,2,3)
      twoDimension(2) = Array(1,2,3,4,5)
      
      For i = 0 To UBound(twoDimension)
        For j = 0 To UBound(twoDimension(i))
          WScript.Echo "(" & i & "," & j & ") = " & twoDimension(i)(j)
        Next
      Next
      

      【讨论】:

        猜你喜欢
        • 2011-02-04
        • 2012-05-03
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-24
        相关资源
        最近更新 更多