2011-10-14 18 views
7

Sto scrivendo un ciclo per passare attraverso la prima serie di un ciclo in 2D, e attualmente hanno in questo modo:ciclo For Each su una matrice 2D in VB.NET

For Each Dir_path In MasterIndex(, 0) 
    'do some stuff here 
Next 

Ma mi sta dando un errore, dicendo che si aspetta un'espressione nel primo campo. Ma questo è quello che sto cercando di fare, scorrere il primo campo. Come posso risolvere questo? Cosa metterei lì dentro?

EDIT: per chiarire, sto cercando specificamente per l'elemento 0a nel sottoarray di ogni matrice, è per questo che secondo campo è in costante 0.

+0

ho aggiornato entrambi gli esempi per spiegare la richiesta più dettagliata – JoshHetland

risposta

14

Ciò può essere eseguito con cicli for innestati

Nota: Quando si utilizza un ciclo For Each per iterare elementi di un array, il segnaposto generato ad ogni iterazione è una copia del valore nella matrice reale. Le modifiche a quel valore non si rifletteranno nell'array originale. Se vuoi fare qualcosa di diverso da leggere le informazioni dovrai usare un ciclo For per indirizzare direttamente gli elementi dell'array.

Assumendo un array a due dimensioni, il seguente esempio di codice assegnerà un valore a ciascun elemento in ogni dimensione.

Dim MasterIndex(5, 2) As String 

For iOuter As Integer = MasterIndex.GetLowerBound(0) To MasterIndex.GetUpperBound(0) 
    'iOuter represents the first dimension 
    For iInner As Integer = MasterIndex.GetLowerBound(1) To MasterIndex.GetUpperBound(1) 
    'iInner represents the second dimension 
    MasterIndex(iOuter, iInner) = "This Isn't Nothing" 'Set the value 
    Next 'iInner 

    'If you are only interested in the first element you don't need the inner loop 
    MasterIndex(iOuter, 0) = "This is the first element in the second dimension" 
Next 'iOuter 
'MasterIndex is now filled completely 

Si potrebbe eventualmente utilizzare la proprietà .Rank per scorrere dinamicamente su ogni dimensione

Se si vuole ciclare su una matrice irregolare come Konrad Rudolph è stato suggerendo (Questo funzionalmente più si avvicina implementazioni di array in altri più liberamente lingue digitati come PHP) si potrebbe andare a questo proposito in questo modo:

'This is a jagged array (array of arrays) populated with three arrays each with three elements 
Dim JaggedIndex()() As String = { 
    New String() {"1", "2", "3"}, 
    New String() {"1", "2", "3"}, 
    New String() {"1", "2", "3"} 
} 

For Each aOuter As String() In JaggedIndex 
    'If you are only interested in the first element you don't need the inner for each loop 
    Dim sDesiredValue As String = aOuter(0) 'This is the first element in the inner array (second dimension) 

    For Each sElement As String In aOuter 
    Dim sCatch As String = sElement 'Assign the value of each element in the inner array to sCatch 
    sElement = "This Won't Stick" 'This will only hold value within the context of this loop iteration 
    Next 'sElement 
Next 'aOuter 
'JaggedIndex is still the same as when it was declared 
1

semplicemente non si può. Gli array multidimensionali non sono realmente supportati nell'infrastruttura framework .NET. Sembrano essere etichettati come un ripensamento. La soluzione migliore è spesso non usarli e utilizzare invece array frastagliati (matrici di array - Integer()() anziché Integer(,)).

+0

così ho cambiato in 'MasterIndex()()', ma quando ho ri posto 'Per ogni Dir_path In MasterIndex (, 0)' con 'Per ogni Dir_path In MasterIndex() (0)' Ottengo 'Il numero di indici è inferiore al numero di dimensioni dell'array indicizzato'? – jayjyli

+1

@Tim Quella sintassi è semplicemente non valida. Usa 'Per ogni x In MasterIndex ... DirPath = x (0)' e lavora da lì ... –