Bạn có thể thực hiện điều này với lồng nhau Ví vòng
Lưu ý: Khi sử dụng lệnh For Each vòng lặp để lặp qua các yếu tố trong một mảng, giữ chỗ tạo ra trên mỗi lần lặp là một bản sao của giá trị trong mảng thực tế. Các thay đổi đối với giá trị đó sẽ không được phản ánh trong mảng ban đầu. Nếu bạn muốn làm bất cứ điều gì khác hơn là đọc thông tin bạn sẽ cần phải sử dụng vòng lặp For để xử lý trực tiếp các phần tử mảng.
Giả sử mảng hai chiều, ví dụ mã sau sẽ gán giá trị cho từng phần tử trong mỗi thứ nguyên.
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
Bạn tùy chọn có thể sử dụng .Rank
tài sản để tự động lặp qua mỗi chiều
Nếu bạn muốn để lặp qua một mảng lởm chởm như Konrad Rudolph đã được gợi ý (Đây chức năng phù hợp chặt chẽ hơn nữa việc triển khai mảng trong khác lỏng lẻo hơn ngôn ngữ đánh máy như PHP), bạn có thể đi về nó như vậy:
'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
tôi cập nhật cả hai ví dụ để giải thích cho yêu cầu chi tiết của bạn – JoshHetland