如何:在 Visual Basic 中初始化陣列變數

您可以在 New 子句中包含陣列常值,並指定陣列的初始值,以初始化陣列變數。 您可以指定類型,也可以允許從陣列常值中的值推斷類型。 如需如何推斷類型的詳細資訊,請參閱陣列的<填入具有初始值的陣列>。

使用陣列常值初始化陣列變數

  • 無論在 New 子句中,或在指派陣列值時,請一律將元素值括在大括號 ({}) 之中。 下列範例示範的幾種方法,可以宣告、建立及初始化變數,以將類型為 Char 之元素的陣列包含在其中。

    ' The following five lines of code create the same array.
    ' Preferred syntaxes are on the lines with chars1 and chars2.
    Dim chars1 = {"%"c, "&"c, "@"c}
    Dim chars2 As Char() = {"%"c, "&"c, "@"c}
    
    Dim chars3() As Char = {"%"c, "&"c, "@"c}
    Dim chars4 As Char() = New Char(2) {"%"c, "&"c, "@"c}
    Dim chars5() As Char = New Char(2) {"%"c, "&"c, "@"c}
    

    每項陳述式在執行之後,所建立的陣列長度為 3,而包含初始值的元素會在索引 0 到索引 2。 如有提供上限和值,必須包含從索引 0 到上限上之各項元素的值。

    請注意,若已在陣列常值中提供元素值,就無須指定索引上限。 若未指定上限,將會依據陣列常值中的值數量來推斷陣列的大小。

使用陣列常值初始化多維度陣列變數

  • 在大括號 ({}) 內巢狀排列值。 確定巢狀陣列常值都會推斷為相同類型和長度的陣列。 下列程式碼範例是幾個多維度陣列初始化的範例。

    Dim numbers = {{1, 2}, {3, 4}, {5, 6}}
    Dim customerData = {{"City Power & Light", "http://www.cpandl.com/"},
                        {"Wide World Importers", "http://wideworldimporters.com"},
                        {"Lucerne Publishing", "http://www.lucernepublishing.com"}}
    
    ' You can nest array literals to create arrays that have more than two 
    ' dimensions.
    Dim twoSidedCube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}
    
  • 您可以明確指定陣列界限,也可以將之排除,讓編譯器依據陣列常值中的值推斷陣列界限。 如有提供上限和值,必須包含每個維度中,從索引 0 到上限之各項元素的值。 下列範例示範的幾種方法,可宣告、建立和初始化變數,以將類型為 Short 之元素的二維陣列包含在其中

    ' The following five lines of code create the same array.
    ' Preferred syntaxes are on the lines with scores1 and scores2.
    Dim scores1 = {{10S, 10S, 10S}, {10S, 10S, 10S}}
    Dim scores2 As Short(,) = {{10, 10, 10}, {10, 10, 10}}
    
    Dim scores3(,) As Short = {{10, 10, 10}, {10, 10, 10}}
    Dim scores4 As Short(,) = New Short(1, 2) {{10, 10, 10}, {10, 10, 10}}
    Dim scores5(,) As Short = New Short(1, 2) {{10, 10, 10}, {10, 10, 10}}
    

    每項陳述式在執行之後,所建立的陣列會包含六個初始化元素,而且全都具有索引 (0,0)(0,1)(0,2)(1,0)(1,1)(1,2)。 每個陣列位置都包含值 10

  • 下列範例會使逐一查看多維陣列。 在使用 Visual Basic 撰寫的 Windows 主控台應用程式中,將程式碼貼至 Sub Main() 方法中。 最後一個註解會顯示輸出。

    Dim numbers = {{1, 2}, {3, 4}, {5, 6}}
    
    ' Iterate through the array.
    For index0 = 0 To numbers.GetUpperBound(0)
        For index1 = 0 To numbers.GetUpperBound(1)
            Debug.Write(numbers(index0, index1).ToString & " ")
        Next
        Debug.WriteLine("")
    Next
    ' Output
    '  1 2
    '  3 4
    '  5 6
    

使用陣列常值初始化不規則陣列變數

  • 在大括號 ({}) 內巢狀排列物件值。 您也可以巢狀排列指定不同長度的陣列常值,但在不規則陣列中,請務必為巢狀陣列常值括上括弧 (())。 括弧會強制評估巢狀陣列常值,並將產生的陣列用為不規則陣列的初始值。 下列程式碼範例顯示兩個不規則陣列初始化的範例。

    ' Create a jagged array of arrays that have different lengths.
    Dim jaggedNumbers = {({1, 2, 3}), ({4, 5}), ({6}), ({7})}
    
    ' Create a jagged array of Byte arrays.
    Dim images = {New Byte() {}, New Byte() {}, New Byte() {}}
    
  • 下列範例會逐一查看不規則陣列。 在使用 Visual Basic 撰寫的 Windows 主控台應用程式中,將程式碼貼至 Sub Main() 方法中。 程式碼的最後一個註解會顯示輸出。

    ' Create a jagged array of arrays that have different lengths.
    Dim jaggedNumbers = {({1, 2, 3}), ({4, 5}), ({6}), ({7})}
    
    For indexA = 0 To jaggedNumbers.Length - 1
        For indexB = 0 To jaggedNumbers(indexA).Length - 1
            Debug.Write(jaggedNumbers(indexA)(indexB) & " ")
        Next
        Debug.WriteLine("")
    Next
    
    ' Output:
    '  1 2 3 
    '  4 5 
    '  6
    '  7
    

另請參閱