Array
An array is a collection of elements of the same data type. In the Go language, it is determined that a statement from an array, you can modify the array members use, but can not change the array size.
Array definition:
var array variable [number of elements] T
// define a length of 3 elements of an array of type int A var A [3] int
note:
package main import "fmt" func main() { var in [3 ] int var b [5 ] int fmt.Println(a, b) // [0 0 0] [0 0 0 0 0]
a = b //不可以这样做,因为此时a和b是不同的类型
// Note: single Both a and b are the arrays are arrays of different types, depending on its length
// array type = type of data within an array of defined length +
}
Array initialization
-
method one
package main import "fmt" func main() { // var a1 [3]int = [3]int{1,2,3} var a1 = [3]int{1,2,3} var a2 = [5]int{1,2,3} var s1 = [3]string{"上海"} FMT .Println (A1) // [2. 3. 1] fmt.Println (A2) // [2. 3. 1 0 0] the number of data in the int type array smaller than the number defined, the default position with zero padding fmt.Println (s1 ) // [Shanghai] the number of data in the array is less than the number of defined string types, the default fill a space bit }
-
Second way
According to the above method ensures consistent initial value every time the length of the array and provides, in general, we have the compiler can infer the length of the array according to the number of its own initial value
package main import "fmt" main FUNC () {
// get the array length automatically var A3 = [...] int {1,2,3,4,5 } var S2 = [...] String { "Shanghai", "Beijing" } FMT .Println (A3) // [2. 1. 4. 3. 5] fmt.Println (S2) // [Shanghai Beijing] }
-
Three ways
We can also use the specified index value way to initialize an array
package main import "fmt" func main() { was a4 [5 ] int A4 = [. 5] int {4: 1} // index number 4 is a remainder 0 fmt.Println (A4) // [0 1 0 0 0]
}
The value of the array
Go array of language can be an index value
package main import "fmt" func main() { var S3 = [...] String { "Shanghai", "Beijing" } fmt.Println(s3[1]) // 北京 fmt.Printf("%T \n", s3[1]) // string s4 := s3[0] fmt .Println (s4) // Shanghai }
Iterate
package main import "fmt" func main() { var s5 = [3]int{1,2,3} for i := 0; i < len(s5); i++{ fmt.Print(s5[i]) // 123 } for i, v := range s5{ FMT . the Printf ( "Index:% d value:% d \ n-", I, V) // index: Value 0: 1 ... } }
Exercise
-
Seeking elements within an array and
package main import "fmt" func main() { was a5 = [...] int {1,3,5 } value := 0 for index := 0; index < len(a5); index++ { value = value + a5[index] } fmt.Println(value) // 9 was V = 0 for _, i: = Range a5 { v = v + i } fmt.Println(value) // 9 }
-
Find [1,3,5,7,8] is a combination of elements and the array 8
package main import "fmt" func main() { var a7 = [...]int{1,3,5,7,8} for i := 0; i < len(a7); i++ { for j := i+1; j < len(a7); j++ { if a7[i]+a7[j] == 8 { fmt.Printf("(%d %d)", a7[i], a7[j]) // (1 7)(3 5) } } } }
Multidimensional (layer) array
Go language support multidimensional arrays, we are here to two-dimensional array, for example (which is nested array of arrays).
Define multidimensional arrays
package main import "fmt" main FUNC () {
// initialize a manner var A9 [. 3] [2 ] int a9 = [3][2]int{ [2]int{1,2}, [2]int{3,4}, } fmt .Println (a8) // [1 2 3] fmt.Println (a9) // [[1 2] [3 4] [0 0]] // initialized two var AlO = [...] [2 ] {int {6,7}, {8,9}, {0,1}, } fmt.Println(a10) // [[6 7] [8 9] [0 1]] // Note: In addition to the first multi-dimensional array can be used [...], other layers can be used not [...] }
The value of multi-dimensional arrays
package main import "fmt" func main() { where a10 = [...] [2 ] {int {6,7}, {8,9}, {0,1}, } fmt.Println(a10[1][0]) // 8 }
Iterate a multidimensional array
package main import "fmt" func main() { where a10 = [...] [2 ] {int {6,7}, {8,9}, {0,1}, } // Method 1 for I: = 0; I <len (AlO); I ++ { FMT . the Print (AlO [I]) // [. 6. 7] [. 8. 9] [0. 1] } // Second way for _, I: = Range AlO { fmt.Print(i) // [6 7][8 9][0 1] } }
Array is a value type
An array is a value type, assignment and transfer of participants to replicate the entire array. Therefore, changing the value of copies, it does not change the value itself.
package main import "fmt" func main() { a11 := [2]int{1,2} a12 : a11 = // variables in memory a12 opened memory space, into an array [2. 1] a11 [0] =. 5 // change a11 array variable [1,2] in values in memory fmt .Println (A11) // [2. 5] fmt.Println (A12) // [2. 1] }