Array and slice 2

Array traversal:

1- conventional way traverse:
  As already mentioned, not repeat them.

Way 2-for-range structure traversing
  this Go is a language unique structure, it can be used to traverse the array element access

The basic syntax:
for Inde, value: = Range array01 {
  ...
}

Description:

  1) The first index is the return value of the array index

  2) The second value is the value in the target position.

  3) they are visible only within the loop of the local variables for

  4) through the array elements when you do not want to use the subscript index, can directly index index marked an underscore _

  . 5) and the index value of the name is not fixed, i.e., the programmer can specify their own, generally designated index value and

 

Case:

func main() {

  // iterate for-range demonstration
  var heroes = [3] string { " Matsue", "Wu", "Lu Junyi"}
  for I, V: = Range Heroes {
    fmt.Printf ( "% I =% VV = V \ n-", I, V)
    fmt.Printf (" Heroes [D%] V =% \ n-", I, Heroes [I]) // print transfer method, using the above recommended
  }

  for _, V: = Range {Heroes
    fmt.Printf ( "% V element value = \ n-", V)
  }
}

 

 Notes and details of the array:

 

1) An array is a combination of a plurality of the same data type, an array declaration once / defined, its length is fixed and can not change dynamically.

main FUNC () {
  var arr01 [. 3] int
  arr01 [0] =. 1
  arr01 [. 1] = 30
  // arr01 [2] = 1.1 // being given here, the data type is not uniform
  arr01 [2] = 90
  // thereof length is fixed and can not dynamically change, or messages bounds
  // arr01 [3] = 80 arrays can not grow dynamically

  fmt.Println(arr01)
}

2) var arr [] int arr time slice is a slice, the slice devoted to the back.

3) elements in the array may be any type of data, including the value and reference types, but can not mix.

4) create an array, if no assignment has a default value (zero value)

  Array Value Type: Default is 0
  string array: The default value is ""
  BOOL array: default value to false

FUNC main () {
  // value. 1 (series of integers, floating point series) => 0
  . @ 2 characters string ==> ""
  // 3. boolean ==> to false
  var arr01 [3] float32
  var arr02 [3] String
  var arr03 [3] BOOL
  fmt.Printf ( "arr01 arr02 = V =% V% =% arr03 V \ n-", arr01, arr02, arr03)
}

5) Step 1. declare an array using an array of open space and 2. assignment 3. Using an array of individual elements to the array

6) the array indices start at zero.

7) array subscript must be used within the specified range, otherwise reported panic: array bounds, such as: var arr [5] int is the effective index 0-4

8) Go genus array value types, the default value is transmitted, thus a value copy. Do not affect each other array.

func test01(arr [3]int) {
  arr[0] = 88
}

func main() {
  arr := [3]int{11, 22, 33}
  test01(arr)
  fmt.Println("main arr=", arr)
}

9) To the other function, to modify the original array, can pass by reference (pointer mode)

func test02(arr *[3]int) {
  (*arr)[0] = 88
}

func main() {
  arr := [3]int{11, 22, 33}
  test02(&arr)
  fmt.Println("main arr=", arr)
}

10) is a part of the length of the array type, when the transfer function parameters, need to consider the length of the array, see the following example:

 

Question 1:
compilation error, because you can not put [3] int passed to [] int

Question 2:
compilation error, because you can not put [3] int passed to [4] int, go think this is the language the two data types. So do not match.

Problem 3:
The correct

Applications array:

1) create an array of 26 elements of a type byte placed 'A', respectively, - 'Z'. Use a for loop to access all the elements and print them out. Tip: The character data operation 'A' + 1 -> 'B'

func main() {

  Thinking //
  // declare an array 1 myChars var [26 is] byte.
  @ 2 using a for loop, the characteristics of operation using the character can be assigned to 'A' + 1 -.> 'B'
  @. 3 used for printing. to

  var myChars [26]byte
  for i := 0; i < 26; i++ {
    myChars[i] = 'A' + byte(i) //注意需要将 i=>byte
    fmt.Printf("%c ", myChars[i])
  }
}

2)请求出一个数组的最大值,并得到对应的下标

func main() {

  //思路
  //1.声明一个数组 var intArr[5] = [...]int {1, -1, 9, 90, 11}
  //2.嘉定第一个元素就是最大值,下标就0
  //3.然后从第二个元素开始循环比较,如果发现有更大,则交换

  var intArr = [...]int {1, -1, 9, 90, 11}
  maxVal := intArr[0]
  maxValIndex := 0
  for i := 1; i < len(intArr); i++ {
    if maxVal < intArr[i] {
      maxVal = intArr[i]
      maxValIndex = i
    }
  }
  fmt.Printf("maxVal=%v maxValIndex=%v \n", maxVal, maxValIndex)
}

3)请求出一个数组的和和平均值。 for-range

func main() {

  //思路
  //1.声明一个数组
  //2.求出和sum
  //3.求出平均值
  var intArr2 = [...]int {1, -1, 9, 90, 12}
  sum := 0
  for _, val := range intArr2 {
    //累计求和
    sum += val
  }

  fmt.Printf("sum=%v 平均值=%v\n", sum, float64(sum) / float64(len(intArr2)))
}

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11355424.html