(200-06-11) VB.NET 教學 陣列Array

摘要:(200-06-11) VB.NET 教學 陣列Array

陣列(使用herp空間)

1.同類型資料集合

2.任何型別均可定義

3.決定元素數目,動態配置記憶體(Herp空間)

4.一律為參考型別  參考型別:STACK定義位址都指向同一組HERP內容

5.元素數量不可變(順序性)

6.陣列宣告時會自動 binding ArrayClass 的Shered Method的Shered Method

列:

 


Module TestArray
    '值行別陣列
    Public Sub Main()
        '區域變數在stack
        '陣列元素在Heap堆積(堆六個元素) 一定有預設值
        Dim iar(5) As Int32
        'Binding(繫結) Sytem.Array
        System.Console.WriteLine(iar.Length)
        '傳遞陣列(陣列參考型別 傳遞位址)
        passing(iar)
        '順序迴圈 陣列元素從零開始
        For i As Int32 = 0 To iar.Length - 1
            System.Console.WriteLine(iar(i))
        Next

        Dim iar2() As Int32 = iar
        iar2(2) = 1000
        For i As Int32 = 0 To iar.Length - 1
            System.Console.WriteLine("New:" + iar(i).ToString())
        Next

    End Sub

    '處理陣列
    Private Sub passing(ByVal iar2() As Int32)
        System.Console.WriteLine(iar2.GetHashCode())
        For i As Int32 = 0 To iar2.Length - 1
            iar2(i) = (i + 1) * 100
        Next
    End Sub

End Module

陣列初始化

 


Module TestArrayInitlailzer
    '陣列定義同時元素初始化
    Public Sub Main()
        'iar(不設定數量) 
        Dim iar() As Int32 = {1, 2, 3, 4, 5, 6, Convert.ToInt32("A"c)}
        '逐一參考
        'Dim i As Int32
        For Each i As Int32 In iar
            System.Console.WriteLine(i)
        Next

        Dim iar2() As Int32 = Nothing
        '.....準備值
        iar2 = New Int32() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
        For Each i As Int32 In iar2
            System.Console.WriteLine(i)
        Next

    End Sub
End Module