【问题标题】:For Each loop on a 2D array in VB.NET对于 VB.NET 中二维数组上的每个循环
【发布时间】:2011-12-07 20:55:06
【问题描述】:

我正在编写一个循环来遍历 2D 循环的第一个数组,我目前有这样的:

For Each Dir_path In MasterIndex(, 0)
    'do some stuff here
Next

但它给了我一个错误,说它需要第一个字段中的表达式。但这就是我想要做的,遍历第一个字段。我该如何解决?我会在里面放什么?

编辑:澄清一下,我专门寻找每个数组的子数组中的第 0 个元素,这就是为什么第二个字段始终为 0。

【问题讨论】:

  • 我更新了这两个示例,以满足您更详细的要求

标签: vb.net foreach iteration multidimensional-array


【解决方案1】:

您可以使用嵌套的 For 循环来完成此操作

注意:当使用 For Each 循环迭代数组中的元素时,每次迭代生成的占位符是实际数组中值的副本。对该值的更改不会反映在原始数组中。如果你想做除了阅读信息之外的任何事情,你需要使用 For 循环来直接寻址数组元素。

假设一个二维数组,下面的代码示例将为每个维度中的每个元素分配一个值。

Dim MasterIndex(5, 2) As String

For iOuter As Integer = MasterIndex.GetLowerBound(0) To MasterIndex.GetUpperBound(0)
  'iOuter represents the first dimension
  For iInner As Integer = MasterIndex.GetLowerBound(1) To MasterIndex.GetUpperBound(1)
    'iInner represents the second dimension
    MasterIndex(iOuter, iInner) = "This Isn't Nothing" 'Set the value
  Next 'iInner

  'If you are only interested in the first element you don't need the inner loop
  MasterIndex(iOuter, 0) = "This is the first element in the second dimension"
Next 'iOuter
'MasterIndex is now filled completely

您可以选择使用.Rank 属性来动态迭代每个维度

如果您想像 Konrad Rudolph 建议的那样遍历锯齿状数组(这在功能上更接近于其他类型更松散的语言(如 PHP)中的数组实现),您可以这样做:

'This is a jagged array (array of arrays) populated with three arrays each with three elements
Dim JaggedIndex()() As String = {
  New String() {"1", "2", "3"},
  New String() {"1", "2", "3"},
  New String() {"1", "2", "3"}
}

For Each aOuter As String() In JaggedIndex
  'If you are only interested in the first element you don't need the inner for each loop
  Dim sDesiredValue As String = aOuter(0) 'This is the first element in the inner array (second dimension)

  For Each sElement As String In aOuter
    Dim sCatch As String = sElement 'Assign the value of each element in the inner array to sCatch
    sElement = "This Won't Stick" 'This will only hold value within the context of this loop iteration
  Next 'sElement
Next 'aOuter
'JaggedIndex is still the same as when it was declared

【讨论】:

    【解决方案2】:

    你根本做不到。 .NET 框架基础结构并不真正支持多维数组。它们似乎被贴上了事后的标签。最好的解决方案通常是不使用它们,而是使用锯齿状数组(数组的数组 - Integer()() 而不是 Integer(,))。

    【讨论】:

    • 所以我把它改成了MasterIndex()(),但是当我用For Each Dir_path In MasterIndex()(0)替换For Each Dir_path In MasterIndex(, 0)时,我得到Number of indices is less than number of dimensions of the indexed array?
    • @Tim 该语法根本无效。使用For Each x In MasterIndex … DirPath = x(0) 并从那里开始工作……
    【解决方案3】:

    您可以递归地使用Enumerable.Range 来迭代数组的维度。

    假设我们有一个 Int 的二维网格(行和列)。

    我们可以这样迭代:

    using System.Linq;
    
    [TestMethod]
    public void TestTwoDimensionalEnumeration()
    {
        int rowcount = 9;
        int columncount = 9;
        int[,] grid = new int[rowcount, columncount];
        var enumerated =
            Enumerable.Range(0, rowcount - 1).
            SelectMany(ri => Enumerable.Range(0, columncount - 1).
            Select(ci => new {
                                RowIndex = ri,
                                ColumnIndex = ci,
                                Value = grid[ri,ci]
                             }));
        foreach (var item in enumerated)
        {
            System.Diagnostics.Trace.WriteLine("Row:" + item.RowIndex + 
                                              ",Column:" + item.ColumnIndex + 
                                              ",Value:" + item.Value);
        }
    }
    

    同样的逻辑可以应用于任意数量的维度。

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      相关资源
      最近更新 更多