如何迭代锯齿状数组?

时间:2020-03-05 18:49:35  来源:igfitidea点击:

这已经让我发疯了几天。为什么以下工作不起作用?

Dim arr(3, 3) As Integer

    For y As Integer = 0 To arr.GetLength(0) - 1
        For x As Integer = 0 To arr.GetLength(y) - 1
            arr(y, x) = y + x
        Next
    Next

另外,如果数组看起来像这样呢?

{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}

解决方案

回答

arr.GetLength(y)

应该

arr.GetLength(1)

回答

因为没有" 2"或者" 3"维。应该是.GetLength(1)而不是.GetLength(y)

另外:在VB.Net中,数组声明的工作方式略有不同。我们在声明中指定的下标是最后一个索引,而不是像用Cor C ++创建的项数。但是该数组仍然像Cor C ++一样是0索引的,而不是像VB6那样是1索引的。这意味着,如果从另一种语言迁移到VB.Net,则无论是哪种语言,数组本能都可能是错误的。在VB.Net中,Dim arr(3,3)As Integer实际上创建了一个4x4数组。

回答

好吧,如果我有一个看起来像这样的数组怎么办

{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}

GetLength(1)仍将如何知道每一行的长度?

基本上我想要的是...一种在任何给定行中查找元素数量的方法。

回答

声明:DIM arr(3,3)Integer allready已指定在任何给定的行中有3个元素(或者4,我不太确定VB)

我们可以尝试:

Dim arr(3) as Integer()

然后,我们应该可以执行以下操作:

arr(n).Length

查找第n行的长度。

我对VB6有点生疏,从未学习过VB.NET,但这应该为我们提供一个"锯齿状"的数组。查看有关多维数组的msdn文档。

回答

Dim arr(3, 3) As Integer
Dim y As Integer
Dim x As Integer

For x = 0 To arr.Rank - 1
    For y = 0 To arr.GetLength(x) - 2
        arr(x, y) = x + y
    Next
Next

上面的代码为我工作。

编辑,但是代码感觉很脏。我想知道我们要完成什么?

回答

好的,所以我们真正需要的是一个"锯齿状数组"。这将使我们拥有一个"包含其他不同长度数组的数组"。

Dim arr As Integer()() = {New Integer() {1, 2, 3}, New Integer() {4, 5, 6, 7, 8, 9, 9, 9}, New Integer() {5, 4, 3, 2}}

  For x = 0 To arr.GetUpperBound(0)
      Console.WriteLine("Row " & x & " has " & arr(x).GetUpperBound(0) & " columns")
      For y = 0 To arr(x).GetUpperBound(0)
          Console.WriteLine("(" & x & "," & y & ") = " & arr(x)(y))
      Next
   Next

输出:

Row 0 has 2 columns
(0,0) = 1
(0,1) = 2
(0,2) = 3
Row 1 has 7 columns
(1,0) = 4
(1,1) = 5
(1,2) = 6
(1,3) = 7
(1,4) = 8
(1,5) = 9
(1,6) = 9
(1,7) = 9
Row 2 has 3 columns
(2,0) = 5
(2,1) = 4
(2,2) = 3
(2,3) = 2

回答

这段代码是Cis以获得锯齿状数组中所有项的组合:

static void Main(string[] args)
    {
        bool exit = false;
        int[] indices = new int[3] { 0, 0, 0 };
        string[][] vectores = new string[3][];

        vectores[0] = new string[] { "A", "B", "C" };
        vectores[1] = new string[] { "A", "B" };
        vectores[2] = new string[] { "B", "D", "E", "F" };

        string[] item;
        int[] tama?os = new int[3]{vectores[0].GetUpperBound(0), 
            vectores[1].GetUpperBound(0), 
            vectores[2].GetUpperBound(0)};

        while (!exit)
        {
            item = new string[]{ vectores[0][indices[0]],
                    vectores[1][indices[1]],
                    vectores[2][indices[2]]};

            Console.WriteLine("[{0},{1},{2}]={3}{4}{5}", indices[0], indices[1], indices[2], item[0], item[1], item[2]);
            GetVector(tama?os, ref indices, ref exit);
        }
        Console.ReadKey();
    }

    public static void GetVector(int[] tama?os, ref int[] indices, ref bool exit)
    {
        for (int i = tama?os.GetUpperBound(0); i >= 0; i--)
        {
            if (tama?os[i] > indices[i])
            {
                indices[i]++;
                break;
            }
            else
            {
                //ULTIMO ITEM EN EL ARRAY, VALIDAR LAS OTRAS DIMENSIONES SI YA ESTA EN EL ULTIMO ITEM
                if (!ValidateIndexes(tama?os, indices))
                    indices[i] = 0;
                else
                {
                    exit = true;
                    break;
                }
            }
        }
    }

    public static bool ValidateIndexes(int[] tama?os, int[] indices)
    {
        for (int i = 0; i < tama?os.Length; i++)
        {
            if (tama?os[i] != indices[i])
                return false;
        }
        return true;
    }

输出看起来像
[0,0,0] = AAB
[0,0,1] = AAD
[0,0,2] = AAE
[0,0,3] = AAF
[0,1,0] = ABB
[0,1,1] = ABD
[0,1,2] = ABE
[0,1,3] = ABF
[1,0,0] = BAB
[1,0,1] =差
[1,0,2] = BAE
[1,0,3] = BAF
[1,1,0] = BBB
[1,1,1] = BBD
[1,1,2] = BBE
[1,1,3] = BBF
[2,0,0] = CAB
[2,0,1] =加元
[2,0,2] = CAE
[2,0,3] = CAF
[2,1,0] = CBB
[2,1,1] = CBD
[2,1,2] = CBE
[2,1,3] = CBF