Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Spring Recruit punch card day 6 chapter 9.

Today we will continue to update the Go language learning record.

An array is a container for storing elements. In Go, the length of an array is part of the array type. When defining an array, you must specify the type and capacity (length) of the element to store.

define

var a1 [3]bool
var a2 [4]int

fmt.Printf("a1:%T\na2:%T\n", a1, a2)
Copy the code

Print result:

Array initialization

The default value

Arrays are defined without initialization, and default elements are zero: bool false, integer and floating point 0, and empty string “”

var a1 [3]bool 
var a2 [4]int

// If not initialized: default elements are zero (Boolean: false integer and floating point: 0 string: "")
fmt.Println(a1, a2)
Copy the code

Print result:

Initialization mode 1

The simplest way to initialize is to define a value in braces that matches the length.

var a1 [3]bool 
a1 = [3]bool{true.false.false}
fmt.Println(a1)
Copy the code

Print result:

Initialization method 2: Automatically determines the array length based on the initial value

Write the length in brackets. When the number of values defined is smaller than the length, the default values will be used, for example: 0, false, “”

a8 := [10]int{0.1.2.3.4.5.6.7}  //7 will be complemented by 0
fmt.Println(a8)
Copy the code

[0 1 2 3 4 5 6 7 0 0]

[…]. The use of the

[…]. When you set the array length, the system automatically determines the array length based on the initial value

aa := [...]int{0.1.2.3.4.5.6.7} / / [...]. Automatically determines the length of the array based on the initial value
fmt.Println(aa)
Copy the code

Print result: [0 1 2 3 4 5 6 7]

Initialization method 3: Initialize by index

If the index is specified, the value will be filled with default values, such as 0, false, “”.

a3 := [5]int{0: 1.4: 2} // Initialize by index
fmt.Println(a3)
Copy the code

Print result: [1 0 0 0 2]

The values

Through the array

Fori loops through the array

citys := [...]string{"Beijing"."Shanghai"."Shenzhen"} // Index from 0 to 2
// traverse by index
for i := 0; i < len(citys); i++ {
   fmt.Println(citys[i])
}
Copy the code

Print result:

For the range traversal

For range is easier to traverse

citys := [...]string{"Beijing"."Shanghai"."Shenzhen"} // Index from 0 to 2
for i, city := range citys {
   fmt.Printf("Key value: %d city: %v\n", i, city)
}
Copy the code

Print result:

Multidimensional array

define

For example, if we need to define a two-dimensional array like [[1, 2, 3][4, 5, 6]], how do we define it?

The following is an example:

  1. The first unit of length [2] in the following code indicates how many elements there are in a two-dimensional array
  2. The second unit of length [3] indicates how many elements there are in the subset array
  3. When initialized: variable = array type {}
// Define a multidimensional array
var a11 [2] [3]int

// Initialize the multidimensional array
a11 = [2] [3]int{[3]int{1.2.3},3]int{4.5.6}, // Note: This last one should also be separated by a comma
}

fmt.Println(a11)
Copy the code

Print result:

The values

Traversal of multidimensional arrays

// Define a multidimensional array
var a11 [2] [3]int

// Initialize the multidimensional array
a11 = [2] [3]int{[3]int{1.2.3},3]int{4.5.6}, // Note: This last one should also be separated by a comma
}

// Double for range traverses the value
for _, v1 := range a11 {
   fmt.Println(v1)
   for _, v2 := range v1 {
      fmt.Println(v2)
   }
}
Copy the code

Print result:

Array features: Value types are not reference types

We found that if we assign b1 to B2 and then modify B2, b1 doesn’t change. I think this is the biggest difference between arrays and slicing

b1 := [3]int{1.2.3}
b2 := b1
b2[0] = 100
fmt.Println(b1,b2)
Copy the code

Print result:

B2 :=b1; b2:=b1; b2:=b1;

An array of actual combat

Find the sum of all elements of the array cArray[1,3,5,7,8]

cArray := [...] Int {1, 3, 5, 7, 8} r := 0 for _, i2 := range cArray {r += i2} FMT.Printf(" %v", r)Copy the code

Print result: Add result: 24

Find the subscripts of the cArray array that sum to 8, such as [0 3] and [1 2].

for i := 0; i < len(cArray); i++ { for j := 0; j < i; CArray j++ {if cArray [I] + [j] = = 8 {FMT. Printf (" accord with the subscript is: % v, % v \ n ", j, I)}}}Copy the code

Print result:

conclusion

The above is the Go array detailed explanation and actual application, recommend everyone and Go Slice contrast learning, Go Slice detailed explanation and actual combat, deepen understanding.

The last

Thanks for reading and welcome to like, favorites,coin(attention)!!