HOW TO:初始化陣列變數在 Visual BasicHow to: Initialize an Array Variable in Visual Basic

包括陣列常值中初始化陣列變數New子句並指定陣列的初始值。You initialize an array variable by including an array literal in a New clause and specifying the initial values of the array. 您可以指定的型別,或允許它從陣列常值中的值推斷。You can either specify the type or allow it to be inferred from the values in the array literal. 如需如何推斷的類型的詳細資訊,請參閱 < 填入陣列與初始值 」 中陣列For more information about how the type is inferred, see "Populating an Array with Initial Values" in Arrays.

若要使用陣列常值初始化陣列變數To initialize an array variable by using an array literal

  • 處於New子句,或當您指派陣列值,提供在括號內的項目值 ({})。Either in the New clause, or when you assign the array value, supply the element values inside braces ({}). 下列範例示範數種方式可宣告、 建立和初始化變數,以包含陣列元素型別為CharThe following example shows several ways to declare, create, and initialize a variable to contain an array that has elements of type 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 包含的起始值的項目。After each statement executes, the array that's created has a length of 3, with elements at index 0 through index 2 containing the initial values. 如果您提供上限和值,您必須包含每個項目,從索引 0 到上限的值。If you supply both the upper bound and the values, you must include a value for every element from index 0 through the upper bound.

    請注意,您不需要指定索引上限,如果您提供陣列常值中的項目值。Notice that you do not have to specify the index upper bound if you supply element values in an array literal. 如果指定沒有上限,則是會依據陣列常值中的值數目來推斷陣列的大小。If no upper bound is specified, the size of the array is inferred based on the number of values in the array literal.

若要使用陣列常值初始化多維陣列變數To initialize a multidimensional array variable by using array literals

  • 巢狀括號內的值 ({}) 大括號內。Nest values inside braces ({}) within braces. 請確認巢狀的陣列常值全都會推斷為相同的類型和長度的陣列。Ensure that the nested array literals all infer as arrays of the same type and length. 下列程式碼範例示範數個多維陣列初始化範例。The following code example shows several examples of multidimensional array initialization.

    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}}}
    
  • 您可以明確指定陣列界限,或省略它們,然後讓編譯器推斷陣列界限依據陣列常值中的值。You can explicitly specify the array bounds, or leave them out and have the compiler infer the array bounds based on the values in the array literal. 如果您提供上限與值,您必須包含每個項目,從索引 0 到上限的值,每個維度。If you supply both the upper bounds and the values, you must include a value for every element from index 0 through the upper bound in every dimension. 下列範例示範數種方式可宣告、 建立和初始化變數,以包含元素為類型的二維陣列 ShortThe following example shows several ways to declare, create, and initialize a variable to contain a two-dimensional array that has elements of type 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)After each statement executes, the created array contains six initialized elements that have indexes (0,0), (0,1), (0,2), (1,0), (1,1), and (1,2). 每個陣列位置都包含值10Each array location contains the value 10.

  • 下列範例中逐一查看多維陣列。The following example iterates through a multidimensional array. 在以 Visual Basic 撰寫的 Windows 主控台應用程式,貼上內部的程式碼Sub Main()方法。In a Windows console application that is written in Visual Basic, paste the code inside the Sub Main() method. 最後一個註解會顯示輸出。The last comments show the output.

    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
    

若要使用陣列常值初始化不規則的陣列變數To initialize a jagged array variable by using array literals

  • 巢狀括號內的物件值 ({})。Nest object values inside braces ({}). 雖然您也可以巢狀陣列常值,指定陣列的長度不同,在不規則的陣列,請確定巢狀的陣列常值會括在括號中 (())。Although you can also nest array literals that specify arrays of different lengths, in the case of a jagged array, make sure that the nested array literals are enclosed in parentheses (()). 括號會強制評估巢狀的陣列常值和產生的陣列做為不規則陣列的初始值。The parentheses force the evaluation of the nested array literals, and the resulting arrays are used as the initial values of the jagged array. 下列程式碼範例示範兩個不規則的陣列初始化範例。The following code example shows two examples of jagged array initialization.

    ' 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() {}}
    
  • 下列範例中逐一查看不規則陣列。The following example iterates through a jagged array. 在以 Visual Basic 撰寫的 Windows 主控台應用程式,貼上內部的程式碼Sub Main()方法。In a Windows console application that is written in Visual Basic, paste the code inside the Sub Main() method. 在程式碼中的最後一個註解會顯示輸出。The last comments in the code show the output.

    ' 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
    

另請參閱See also