摘要:(200-06-14) VB.NET 教學
陣例(Array)
- 維度不限制
陣列多維度宣告
Module TestMultiDim
'多維度
Public Sub Main()
'定義區域變數
Dim iar(3, 3) As Int32
System.Console.WriteLine(iar.Length) '問總元素數量 16
System.Console.WriteLine(iar.GetLength(0))
'assign
iar(0, 0) = 100
iar(0, 1) = 200
iar(0, 2) = 300
iar(0, 3) = 400
'次數迴圈
'GetLength(維度) 元素數量 也是從零開始
For i As Int32 = 0 To iar.GetLength(0) - 1
For k As Int32 = 0 To iar.GetLength(1) - 1
System.Console.WriteLine(iar(i, k).ToString())
Next
Next
End Sub
End Module
多維度初始化
Module TestMultiDimInitializer
Public Sub Main()
'定義區域變數 ,初始化
Dim iar(,) As Int32 = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
System.Console.WriteLine(iar.Length)
For i As Int32 = 0 To iar.GetLength(0) - 1
For k As Int32 = 0 To iar.GetLength(1) - 1
System.Console.WriteLine(iar(i, k).ToString())
Next
Next
End Sub
End Module
不規則維度定義
Module TestRaggDimen
Public Sub Main()
Dim iar(3)() As Int32
System.Console.WriteLine(iar.Length)
'陣列中陣列
iar(0) = New Int32() {1, 2, 3, 4, 5}
iar(1) = New Int32() {1, 2, 3, 4, 5, 6}
iar(2) = New Int32() {1, 2, 3, 4, 5, 6, 7}
iar(3) = New Int32() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
System.Console.WriteLine(iar.Length) '第一層
For i As Int32 = 0 To iar.Length - 1
For k As Int32 = 0 To iar(i).Length - 1
System.Console.WriteLine(iar(i)(k).ToString())
Next
Next
End Sub
End Module