这几天来这让我抓狂。为什么以下不起作用?

    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}
}
有帮助吗?

解决方案

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

  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”或“3”维度。应该是 .GetLength(1) 而不是 .GetLength(y)

还:在 VB.Net 中,数组声明的工作方式略有不同。您在声明中指定的下标是最后一个索引,而不是像 C# 或 C++ 那样创建的项数。但该数组仍然是像 C# 或 C++ 那样的 0 索引,而不是像 VB6 那样的 1 索引。这意味着,如果您从其他语言转向 VB.Net,则无论是哪种语言,您的数组本能都可能是错误的。在 VB.Net 中, Dim arr(3,3) 作为整数 实际上创建了一个 4x4 大批。

arr.GetLength(y)

应该

arr.GetLength(1)

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

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

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


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

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

上面的代码对我有用。

编辑一下,不过代码感觉很脏。我想知道你想实现什么目标?

此 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] =不良[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

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

你可以尝试:

Dim arr(3) as Integer()

然后您应该能够执行以下操作:

arr(n).Length

求第 n 行的长度。

我对 VB6 有点生疏,从来没有学过 VB.NET,但这应该会给你一个“锯齿状”数组。查看有关多维数组的 msdn 文档。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top