<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # **多維數組** 數組可以有多個維度。然而,在沒有嚴肅理由的情況下,使用超過三個維度會使你的程序難于閱讀,并可能產生bug。 >Tip: 數組可以存儲所有類型的元素,這里我們只用整數講解,因為他們更容易理解和類型。 下面的Go代碼演示了如何創建一個二維數組(twoD)和另一個三維數組(threeD): ```go twoD := [4][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},{13, 14, 15, 16}} threeD := [2][2][2]int{{{1, 0}, {-2, 4}}, {{5, -1}, {7, 0}}} ``` 訪問、分配或打印這兩個數組中的一個元素是很容易的。例如,twoD數組的第一個元素是`twoD[0][0]`,它的值是1。 因此,使用多層循環遍歷threeD 的每一個元素可以通過下面的代碼實現: ```GO for i := 0; i < len(threeD); i++ { for j := 0; j < len(v); j++ { for k := 0; k < len(m); k++ { } } } ``` 我們看到,我們需要和數組維度一樣的循環的數量來遍歷所有元素。這個規則對切片`slice`也適用,下章將會講到。這里使用`x, y, z`替代`i, j, k`會比較好。 `usingArrays.go`展示了如何在Go中使用數組,下面分成三部分進行講解。 第一部分的代碼如下: ```Go package main import ( "fmt" ) func main() { anArray := [4]int{1, 2, 4, -4} twoD := [4][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14,15, 16}} threeD := [2][2][2]int{{{1, 0}, {-2, 4}}, {{5,-1},{7, 0}}} ``` 這里我們定義了三個數組變量分別叫 anArray, twoD 和 threeD。 第二部分的代碼如下: ```go fmt.Println("The length of", anArray, "is", len(anArray)) fmt.Println("The first element of", twoD, "is", twoD[0][0]) fmt.Println("The length of", threeD, "is", len(threeD)) for i := 0; i < len(threeD); i++ { v := threeD[i] for j := 0; j < len(v); j++ { m := v[j] for k := 0; k < len(m); k++ { fmt.Print(m[k], " ") } } fmt.Println() } ``` 我們從第一個for循環中得到一個二維數組(`threeD[i]`), 同理,第二個for循環中得到一個一維數組。最后一個for循環遍歷得到的一維數組得到里面的每一個元素。 最后一部分代碼: The last code part comes with the next Go code: ```go for _, v := range threeD { for _, m := range v { for _, s := range m { fmt.Print(s, " ") } } fmt.Println() } } ``` `range`關鍵字的作用與上一個代碼段的for循環中使用的迭代變量完全相同,但它的作用更優雅、更清晰。然而,如果想要提前知道將要執行的迭代次數,那么就不能使用`range`關鍵字。 >`range`關鍵字也適用于Go映射,這使得它非常方便,也是我喜歡的迭代方式。正如將在第9章中看到的Go - Goroutines、channel和pipeline中的并發性,range關鍵字也適用于channel。 執行 `usingArrays.go` 結果輸出如下: ```bash $ go run usingArrays.go The length of [1 2 4 -4] is 4 The first element of [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16]] is 1 The length of [[[1 0] [-2 4]] [[5 -1] [7 0]]] is 2 1 0 -2 4 5 -1 7 0 1 0 -2 4 5 -1 7 0 ``` 數組最大的問題之一是越界錯誤,這意味著試圖訪問數組中不存在的元素。這就像試圖訪問一個只有5個元素的數組的第6個元素。Go編譯器將此認作編譯檢測的編譯問題,因為這有助于開發工作流。因此,Go編譯器能檢測數組越界錯誤并給出如下錯誤提示: ```bash ./a.go:10: invalid array index -1 (index must be non-negative) ./a.go:10: invalid array index 20 (out of bounds for 2-element array) ```
                  <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>

                              哎呀哎呀视频在线观看