<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                {% raw %} # Visual Basic 數組 > 原文: [https://zetcode.com/lang/visualbasic/arrays/](https://zetcode.com/lang/visualbasic/arrays/) 在 Visual Basic 編程教程的這一部分中,我們將介紹數組。 我們將初始化數組并從中讀取數據。 數組是數據的集合。 變量一次只能容納一項。 數組可以容納多個項目。 這些項目稱為數組的元素。 數組存儲相同數據類型的數據。 每個元素都可以由索引引用。 數組從零開始。 第一個元素的索引為零。 集合具有相似的目的。 它們比數組更強大。 它們將在后面描述。 數組用于存儲我們應用的數據。 我們聲明數組為某種數據類型。 我們指定它們的長度。 我們用數據初始化數組。 我們有幾種使用數組的方法。 我們可以修改元素,對其進行排序,復制或搜索。 ## 初始化數組 有幾種方法可以在 Visual Basic 中初始化數組。 ```vb Option Strict On Module Example Sub Main() Dim array(5) As Integer array(0) = 3 array(1) = 2 array(2) = 1 array(3) = 5 array(4) = 6 For i As Integer = 0 To array.Length-1 Console.WriteLine(array(i)) Next End Sub End Module ``` 我們聲明并初始化一個數值數組。 數組的內容將打印到控制臺。 ```vb Dim array(5) As Integer ``` 在這里,我們聲明一個包含五個元素的數組。 所有元素都是整數。 ```vb array(0) = 3 array(1) = 2 ... ``` 我們用一些數據初始化數組。 這是分配初始化。 索引在括號中。 數字 3 將成為數組的第一個元素,數字 2 將成為第二個元素。 ```vb For i As Integer = 0 To array.Length-1 Console.WriteLine(array(i)) Next ``` 我們遍歷數組并打印其元素。 數組具有`Length`屬性,該屬性給出數組中元素的數量。 由于數組基于零,因此索引為`0..length-1`。 我們可以在一個語句中聲明并初始化一個數組。 ```vb Option Strict On Module Example Sub Main() Dim array() As Integer = { _ 2, 4, 5, 6, 7, 3, 2 } For Each i As Integer In array Console.WriteLine(i) Next End Sub End Module ``` 這是先前程序的修改版本。 ```vb Dim array() As Integer = { _ 2, 4, 5, 6, 7, 3, 2 } ``` 一步就聲明并初始化一個數組。 元素在大括號中指定。 我們沒有指定數組的長度。 編譯器將為我們完成此任務。 ```vb For Each i As Integer In array Console.WriteLine(i) Next ``` 我們使用`For Each`關鍵字遍歷數組并打印其內容。 ## 數組的界限 Visual Basic 具有兩個用于獲取數組邊界的函數。 `LBound()`函數返回數組指定維的最低可用下標。 `UBound()`函數返回數組指定維的最高可用下標。 到目前為止,我們已經處理了一維數組。 ```vb Option Strict On Module Example Dim n1 As Integer Dim n2 As Integer Sub Main() Dim names() As String = { "Jane", "Lucy", _ "Timea", "Beky", "Lenka"} n1 = LBound(names) n2 = UBound(names) Console.WriteLine(n1) Console.WriteLine(n2) For i As Integer = n1 To n2 Console.WriteLine(names(i)) Next End Sub End Module ``` 我們有一系列名稱。 我們計算并使用該數組的上下邊界。 ```vb n1 = LBound(names) n2 = UBound(names) ``` 在名稱數組中,`n1`是最低索引,`n2`是最高索引。 ```vb For i As Integer = n1 To n2 Console.WriteLine(names(i)) Next ``` 我們使用數組的上下邊界檢查數組。 ```vb $ ./bounds.exe 0 4 Jane Lucy Timea Beky Lenka ``` 示例的輸出。 ## 數組大小 到目前為止,我們已經處理了一維數組。 指定元素所需的索引數稱為數組的維或等級。 我們將使用二維數組。 ```vb Option Strict On Module Example Sub Main() Dim numbers(,) As Integer = { {2, 1}, {3, 5}, _ {4, 4}, {7, 2}, {0, 0} } For i As Integer = 0 To UBound(numbers, 1) For j As Integer = 0 To UBound(numbers, 2) Console.Write(CStr(numbers(i, j)) + " ") Next j Console.Write(vbNewLine) Next i End Sub End Module ``` 如果我們需要兩個索引來訪問數組中的元素,那么我們將擁有一個二維數組。 ```vb Dim numbers(,) As Integer = { {2, 1}, {3, 5}, _ {4, 4}, {7, 2}, {0, 0} } ``` 我們在一個語句中聲明并初始化一個二維數組。 請注意數組名稱后面括號內的逗號。 ```vb For i As Integer = 0 To UBound(numbers, 1) For j As Integer = 0 To UBound(numbers, 2) Console.Write(CStr(numbers(i, j)) + " ") Next j Console.Write(vbNewLine) Next i ``` 我們需要兩個循環才能從二維數組中獲取數據。 `UBound()`函數具有可選的第二個參數`rank`。 這是我們檢索最高索引的維度。 如果省略等級,則假定為 1 維。 ```vb $ ./twodimensions.exe 2 1 3 5 4 4 7 2 0 0 ``` 代碼示例的輸出。 接下來,我們將處理三維數組。 ```vb Option Strict On Module Example Sub Main() Dim i As Integer Dim j As Integer Dim k As Integer Dim nums(,,) As Integer = { _ {{12, 2, 8}}, _ {{14, 5, 2}}, _ {{3, 26, 9}}, _ {{4, 11, 2}} _ } For i = 0 To UBound(nums, 1) For j = 0 To UBound(nums, 2) For k = 0 To UBound(nums, 3) Console.Write(CStr(nums(i, j, k)) + " ") Next k Next j Console.Write(vbNewLine) Next i End Sub End Module ``` 我們有一個數字三維數組。 同樣,我們用數字初始化數組并將其打印到終端。 ```vb Dim nums(,,) As Integer = { _ {{12, 2, 8}}, _ {{14, 5, 2}}, _ {{3, 26, 9}}, _ {{4, 11, 2}} _ } ``` 左括號和右括號之間還有另一個逗號。 ```vb For k = 0 To UBound(nums, 3) Console.Write(CStr(nums(i, j, k)) + " ") Next k ``` 該循環經過三維。 我們使用三個索引從數組中檢索值。 ```vb $ ./3darray.exe 12 2 8 14 5 2 3 26 9 4 11 2 ``` 我們將三維數組的內容打印到控制臺。 有一個`Rank()`函數,它給出數組的維數。 ```vb Option Strict On Module Example Sub Main() Dim array1() As Integer = {1, 2} Dim array2(,) As Integer = { { 1 }, { 2 } } Dim array3(, ,) As Integer = { { { 1, 2 }, { 2, 1 } } } Console.WriteLine(array1.Rank()) Console.WriteLine(array2.Rank()) Console.WriteLine(array3.Rank()) End Sub End Module ``` 我們有三個數組。 我們使用`Rank()`函數來獲取每個大小的大小。 ```vb Console.WriteLine(array1.Rank()) ``` 在這里,我們獲得第一個數組的排名。 ## 鋸齒狀數組 具有相同大小元素的數組稱為矩形數組。 相反,具有不同大小元素的數組稱為鋸齒狀數組。 鋸齒狀數組的聲明和初始化方式不同。 ```vb Option Strict On Module Example Sub Main() Dim jagged As Integer()() = New Integer(4)() {} jagged(0) = New Integer() {1} jagged(1) = New Integer() {3, 4} jagged(2) = New Integer() {5, 6, 7} jagged(3) = New Integer() {5, 6} jagged(4) = New Integer() {9} For i As Integer = 0 To jagged.GetUpperBound(0) For j As Integer = 0 To jagged(i).GetUpperBound(0) Console.Write(jagged(i)(j) & " ") Next Console.Write(vbNewLine) Next End Sub End Module ``` 這是一個鋸齒狀數組的示例。 ```vb Dim jagged As Integer()() = New Integer(4)() {} ``` 這是鋸齒狀數組的聲明。 我們有一個數組數組。 更具體地說,我們聲明了一個數組,該數組具有五個整數數據類型的數組。 ```vb jagged(0) = New Integer() {1} jagged(1) = New Integer() {3, 4} ... ``` 每個數組必須單獨初始化。 ```vb Console.Write(jagged(i)(j) & " ") ``` 與矩形數組不同,每個索引都用括號括起來。 ## 數組方法 有多種使用數組的方法。 這些方法可用于檢索,修改數據,排序,復制,搜索數據。 我們使用的這些方法是`Array`類的靜態方法或數組對象的成員方法。 ```vb Option Strict On Module Example Sub Main() Dim names() As String = {"Jane", "Frank", "Alice", "Tom" } Array.Sort(names) For Each el As String In names Console.Write(el + " ") Next Console.Write(vbNewLine) Array.Reverse(names) For Each el As String In names Console.Write(el + " ") Next Console.Write(vbNewLine) End Sub End Module ``` 在此示例中,我們對數據進行排序。 ```vb Dim names() As String = {"Jane", "Frank", "Alice", "Tom" } ``` 我們有一個字符串數組。 ```vb Array.Sort(names) ``` `Sort()`方法按字母順序對數據進行排序。 ```vb Array.Reverse(names) ``` `Reverse()`方法反轉整個一維數組中元素的順序。 ```vb $ ./sorting.exe Alice Frank Jane Tom Tom Jane Frank Alice ``` 我們已經按升序和降序對名稱進行了排序。 以下示例使用`SeValue()`,`GetValue()`,`IndexOf()`,`Copy()`和`Clear()`方法。 ```vb Option Strict On Module Example Dim names() As String = {"Jane", "Frank", "Alice", "Tom" } Dim girls(0 To 3) As String Sub Main() names.SetValue("Beky", 1) names.SetValue("Erzebeth", 3) Console.WriteLine(names.GetValue(1)) Console.WriteLine(names.GetValue(3)) Console.WriteLine(Array.IndexOf(names, "Erzebeth")) Array.Copy(names, girls, names.Length) For Each el As String In girls Console.Write(el + " ") Next Console.Write(vbNewLine) Array.Clear(names, 0, 2) For Each el As String In names Console.Write(el + " ") Next Console.Write(vbNewLine) End Sub End Module ``` 本示例介紹了其他方法。 ```vb Dim girls(0 To 3) As String ``` 聲明數組的另一種方法。 ```vb names.SetValue("Beky", 1) names.SetValue("Erzebeth", 3) ``` `SetValue()`為數組中的特定索引設置一個值。 ```vb Console.WriteLine(names.GetValue(1)) Console.WriteLine(names.GetValue(3)) ``` 我們使用`GetValue()`方法從數組中檢索值。 ```vb Console.WriteLine(Array.IndexOf(names, "Erzebeth")) ``` `IndexOf()`方法返回首次出現特定值的索引。 ```vb Array.Copy(names, girls, names.Length) ``` `Copy()`方法將值從源數組復制到目標數組。 第一個參數是源數組,第二個參數是目標數組。 第三個參數是長度; 它指定要復制的元素數。 ```vb Array.Clear(names, 0, 2) ``` `Clear()`方法從數組中清除元素。 它需要三個參數,即數組,起始索引和要從索引中清除的元素數。 在 Visual Basic 教程的這一部分中,我們使用了數組。 我們描述了各種類型的數組和使用它們的方法。 {% endraw %}
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看