簡體   English   中英

如何迭代鋸齒狀數組?

[英]How to iterate a jagged array?

這讓我瘋狂了好幾天。 以下為什么不工作?

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

因為沒有'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數組。

好吧,你真正需要的是一個“鋸齒狀陣列”。 這將允許您擁有一個“包含不同長度的其他數組的數組”。

  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

arr.GetLength(y)

應該

arr.GetLength(1)

那么如果我有一個看起來像這樣的陣列呢?

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

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


基本上我想要的是...... 一種查找任何給定行中元素數量的方法

這個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

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(3,3) As Integer allready指定任何給定行中有3個元素(或者4,我對VB不太確定)

你可以嘗試:

Dim arr(3) as Integer()

那么你應該能夠做到:

arr(n).Length

找到行n的長度。

我在VB6上有點生疏,從來沒有學過VB.NET,但這應該給你一個'鋸齒狀'陣列。 查看有關多維數組的msdn文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM