VB.NET 中二维数组上的 For Each 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7769980/
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-09 15:43:59  来源:igfitidea点击:

For Each loop on a 2D array in VB.NET

vb.netforeachiterationmultidimensional-array

提问by jayjyli

I'm writing a loop to go through the first array of a 2D loop, and I currently have it like this:

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

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

But it's giving me an error, saying it expects an expression in the first field. But that's what I'm trying to do, loop through the first field. How do I fix this? What would I put in there?

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

EDIT: to clarify, I'm specifically looking for the 0th element in the subarray of each array, that's why that second field is constantly 0.

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

回答by JoshHetland

You can accomplish this with nested For loops

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

Note: When using a For Each loop to iterate over elements in an array, the placeholder generated on each iteration is a copy of the value in the actual array. Changes to that value will not be reflected in the original array. If you want to do anything other than read the information you will need to use a For loop to address the array elements directly.

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

Assuming a two dimension array the following code example will assign a value to each element in each dimension.

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

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

You could optionally use the .Rankproperty to dynamically iterate over each dimension

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

If you want to loop over a jagged array like Konrad Rudolph was suggesting (This functionally more closely matches array implementations in other more loosely typed languages like PHP)you could go about it like so:

如果你想像 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

回答by Konrad Rudolph

You simply can't. Multi-dimensional arrays aren't really supported in the .NET framework infrastructure. They seem to be tagged on as an afterthought. The best solution is often not to use them, and to use jagged arrays instead (arrays of arrays – Integer()()instead of Integer(,)).

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

回答by Cogent

You can use Enumerable.Rangerecursively to iterate the dimensions of an array.

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

Lets say we have a two dimensional grid (rows and columns) of Int.

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

We can iterate it as follows:

我们可以按如下方式迭代它:

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);
    }
}

The same logic can be applied to any number of dimensions.

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