2008-09-08 5 views
2

Das hat mich für ein paar Tage verrückt gemacht. Warum funktioniert das Folgende nicht?Wie man ein gezacktes Array iteriert?

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 

Auch, was, wenn das Array wie diese stattdessen sah?

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

Antwort

5

Ok, also was Sie wirklich brauchen, ist eine "gezackte Array". Dies ermöglicht Ihnen ein "Array, das andere Arrays unterschiedlicher Länge enthält".

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 

Ausgang:

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 
4

arr.GetLength(y)

sollte

arr.GetLength(1)

+0

Argh. Du schlägst mich;) –

+0

+ 1 Nice One :) –

7

werden, da '2' oder '3' keine Dimension gibt. Sollte .GetLength (1) anstelle von .GetLength (y) sein

Auch: in VB.Net funktionieren Array-Deklarationen etwas anders. Der Index, den Sie in der Deklaration angeben, ist der letzte Index, nicht die Anzahl der Elemente, die mit C# oder C++ erstellt wurden. Aber das Array ist immer noch 0-indexiert wie C# oder C++ anstatt 1-indiziert wie VB6. Das heißt, wenn Sie von einer anderen Sprache zu VB.Net wechseln, sind Ihre Array-Instinkte wahrscheinlich falsch, egal welche Sprache es ist. In VB.Net, Dim arr (3,3) As Integer erstellt tatsächlich ein 4x4 Array.

1

Nun was ist, wenn ich ein Array hatte die

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

so aussah Wie würden GetLength (1) noch die Länge jeder Zeile wissen?


Im Grunde, was ich will, ist .... eine Art und Weise die Anzahl der Elemente in einem bestimmten Zeile zu finden.

+0

Das ist kein Array (3,3) wie Ihre ursprüngliche Frage auch nicht. –

-1

Ihre Erklärung: DIM arr(3,3) As Integer gibt allready, dass es 3 Elemente in einer bestimmten Zeile (oder 4, ich bin nicht so sicher VB)

Sie könnten versuchen:

Dim arr(3) as Integer() 

sollten Sie dann in der Lage zu tun:

arr(n).Length 

Um die Länge der Zeile n zu finden.

Ich bin ein wenig rostig auf VB6 und lernte nie VB.NET, aber das sollte Ihnen eine "gezackte" Array geben. Überprüfen Sie die msdn-Dokumentation für mehrdimensionierte Arrays.

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 

Der obige Code funktionierte für mich.

Bearbeiten, der Code fühlt sich aber schmutzig. Ich frage mich, was Sie versuchen zu erreichen?

0

Dieser Code en C# ist es, alle Kombinationen von Elementen in einer gezackten Array zu erhalten:

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

Der Ausgang wie sieht [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

Verwandte Themen