共用方式為


如何:在 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
    

另請參閱