Go core development study notes (17) - Array

C arrays and similar array, is a unique slice sliced

Array

  1. Golang array as a value type is defined as follows:

var intArray [3] int // define an array of integers, comprising three elements, wherein the subscript 0,1,2
var flArray [. 3] // define a float64 float64 array, comprising three elements, the initial values are is 0
var intArray [. 3] = int [. 3] int {10,20,30} // define an array of integers, comprising three elements and assigns
var intArray [3] string = [ 3] string { "Kobe", " Durant "," James "} // definition of a string array and assign
var intArray [. 3] = string [. 3] 0 {string:" Kobe ": 2:" Durant ":. 1:" James "} // definition of a string array and assigned subscripts indicate by what corresponds to a value

intArray var [. 3] int
intArray [0] = 10
intArray [. 1] = 20 is
intArray [2] // = 30 or so assigned
var arr [] int // !!! this is not an array, a slice Slice
var intArray1 = [ ...] int {10,20,30} // let the system recognize it, with function parameters args ... Like, three essential points

  1. Open address in memory array contiguous space together, each element of the specific memory space of the data determined, the address of an array is the array a [0] is
    & intArray [0] == & intArray

  2. Array traversal: for-range structure traversal
    for _, value: = range intArray {...} // traverse such an array each element

  3. Array is a combination of a plurality of the same data type, length once the declaration is fixed and can not dynamically change

  4. Array [] xx, xx is a value type may be also be a reference type, but attention must be consistent with the data types, data types can not be different from the composition of the array, i.e., can not be mixed

  5. The default value of the array: int 0; string ""; bool false

  6. To use an array of:
    (1) declare an array variable and open up contiguous memory space
    (2) for the array assignment
    (3) using an array

  7. Array index starts from 0, the subscript must [foo] xx foo-1 number, will be used if the bounds panic (index out of range)

  8. An array is part of a value type, default value is passed, so the value will be copied between the array will not affect each other, calling array variable to modify the value of a variable element in the array will not affect the original array variable

  9. If you wish to modify the array variables of the called function, then directly imparting function pointer array variable is called, by modifying the * (array) [0] = xx array variable changed in a manner

  10. Go is a part of the length of the array type, if a function as a receiving array variable parameter, then the actual parameters and parameter must be consistent length of the array, i.e., [n] int n must wait in long

Array Case

Case I: Create a 26-letter type byte array, placed AZ respectively, use a for loop to access and print them out.

package main
import "fmt"
func main() {
	var myChars [26]byte
	for i := 0 ; i <= 25 ; i++ {
		myChars[i] = 'A' + byte(i)
	}
	for i := 0 ; i<= 25 ; i++ {
	fmt.Printf("%c",myChars)
	}
}

Case II: an array seek the maximum number and subscript

package main

import "fmt"

func main() {
	/*
	思路:一个数组中假设array[0]为最大,下标即为0,初始化变量值;
	      这时从数组中第二个数,也就是下标为1的数开始,for循环逐一比较,凡是比0大,那么我就直接修改初始值为当前下标和对应的值
	 */
	var array = [...]int{10,20,-1,90,220}
	maxValue := array[0]
	maxIndex := 0

	for i :=1 ; i < len(array) ; i++ {
		if maxValue < array[i] {
			maxValue = array[i]
			maxIndex = i
		}
	}
	fmt.Printf("最大值的下标为%v,最大值为%v",maxIndex,maxValue)
}

Case 3: a request and the values ​​in an array, and an average value, for-range

package main

import "fmt"

func main() {
	/*
		只要要把遍历的数组中的值做一个累加即可
	*/
	var array = [...]int{10,20,-1,90,220}
	sum := 0
	for _ , value := range array {
		sum += value
	}
	fmt.Printf("和为%v\n",sum)
	fmt.Printf("平均数为%v\n",float64(sum) / float64(len(array)))  // 凡是涉及到平均值包含小数的情况,最后一定要保证除数和被除数都是一个数据类型
}

Case four: five random number generation, and reverse print

package main
import (
	"fmt"
	"math/rand"
	"time"
)
func main() {
	/*
	生成五个随机数需要使用rand.intn()函数,但由于存在seed不变情况,所以需要使用时间函数来使seed随时在变化,保证随机数每次不一致
	 */
	var intArray [5]int
	//记得seed变化一定是纳秒最好,因为秒有些时候因为操作在1秒内,完全有可能一致,所以Unix()不推荐
	rand.Seed(time.Now().UnixNano())
	for i := 0 ; i < len(intArray) ; i++ {
		intArray[i] = rand.Intn(100)
	}
	fmt.Println(intArray)

	/*
	交换次数: len(intArray) / 2  无论奇数还是偶数,因为整数的除法是取模
	 */
	temp := 0
	for i := 0 ; i < len(intArray) / 2 ; i++ {
		temp = intArray[len(intArray) - 1 - i]
		intArray[len(intArray) - 1 - i] = intArray[i]
		intArray[i] = temp
	}
	fmt.Println(intArray)
}
Published 50 original articles · won praise 18 · views 4012

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89819944