Domanda

Questa cosa mi sta facendo impazzire da qualche giorno.Perché quanto segue non funziona?

    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

Inoltre, cosa succederebbe se invece l'array fosse simile a questo?

{ {1, 2, 3},
  {4, 5, 6, 7, 8, 9, 9, 9},
  {5, 4, 3, 2}
}
È stato utile?

Soluzione

Ok, quindi ciò di cui hai veramente bisogno è un "array frastagliato".Ciò ti consentirà di avere un "array che contiene altri array di varia lunghezza".

  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

Produzione:

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

Altri suggerimenti

Perché non esiste una dimensione "2" o "3".Dovrebbe essere .GetLength(1) anziché .GetLength(y)

Anche:in VB.Net le dichiarazioni di array funzionano in modo leggermente diverso.L'indice specificato nella dichiarazione è l'ultimo indice, non il numero di elementi creati come con C# o C++.Ma l'array è ancora indicizzato 0 come C# o C++, anziché indicizzato 1 come VB6.Ciò significa che se passi a VB.Net da una lingua diversa i tuoi istinti di array probabilmente sono sbagliati, indipendentemente dalla lingua.In VB.Net, Dim arr(3,3) Come intero in realtà crea a 4x4 vettore.

arr.GetLength(y)

dovrebbe essere

arr.GetLength(1)

E se avessi un array simile a questo?

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

Come farebbe GetLength(1) a conoscere ancora la lunghezza di ogni riga?


Fondamentalmente quello che voglio è.... un modo per trovare il numero di elementi in una determinata riga.

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

Il codice sopra ha funzionato per me.

Modifica, però il codice sembra sporco.Mi chiedo: cosa stai cercando di realizzare?

Questo codice in C# serve per ottenere tutte le combinazioni di elementi in un array frastagliato:

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

L'output sembra [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

La tua dichiarazione: DIM arr(3,3) As Integer specifica già che ci sono 3 elementi in ogni riga (o 4, non sono così sicuro di VB)

Potresti provare:

Dim arr(3) as Integer()

Dovresti quindi essere in grado di fare:

arr(n).Length

Per trovare la lunghezza della riga n.

Sono un po' arrugginito su VB6 e non ho mai imparato VB.NET, ma questo dovrebbe darti un array "frastagliato".Controlla la documentazione di msdn sugli array multidimensionali.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top