如何迭代锯齿状数组?

时间:2008-09-08 19:12:29

标签: vb.net arrays jagged-arrays

这让我疯狂了好几天。为什么以下不起作用?

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

7 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:5)

好的,你真正需要的是一个“锯齿状阵列”。这将允许您拥有一个“包含不同长度的其他数组的数组”。

  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

答案 2 :(得分:4)

arr.GetLength(y)

应该是

arr.GetLength(1)

答案 3 :(得分:1)

如果我有一个看起来像这样的数组

那该怎么办?
{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}

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


基本上我想要的是...... 找到任何给定行中元素数量的方法

答案 4 :(得分:0)

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

以上代码对我有用。

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

答案 5 :(得分:0)

这段代码C#是获取锯齿状数组中所有项目的组合:

    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] = BAD [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] = CAD [2,0,2] = CAE [2,0,3] = CAF [2,1,0] = CBB [2,1,1] = CBD [2,1,2] = CBE [2,1,3] = CBF

答案 6 :(得分:-1)

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

你可以尝试:

Dim arr(3) as Integer()

然后您应该能够:

arr(n).Length

查找行n的长度。

我在VB6上有点生疏,从未学过VB.NET,但这应该给你一个'锯齿状'阵列。查看有关多维数组的msdn文档。

相关问题