数组 数组是同一个类型的数据集合
基本操作 初始化与遍列1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 func main () { var a [10 ]int fmt.Println("a = " , a) var b [6 ]int = [6 ]int {10 , 20 , 30 , 40 } fmt.Println("b = " , b) c := [6 ]int {10 , 20 , 30 , 40 } fmt.Println("c = " , c) d := [5 ]int {1 , 2 , 3 , 40 , 50 } fmt.Println("d = " , d) e := [5 ]int {0 : 100 , 2 : 'a' , 4 : 10 } fmt.Println("e = " , e) for i := 0 ; i < len (b); i++ { fmt.Printf("index:b[%d] = %d\n" , i, b[i]) } for i, data := range b { fmt.Printf("range:b[%d] = %d\n" , i, data) } }
二维数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 func main () { var a [3 ][4 ]int fmt.Println("a = " , a) data := 0 for i := 0 ; i < 3 ; i++ { for j := 0 ; j < 4 ; j++ { data++ a[i][j] = data } } fmt.Println("a = " , a) b := [3 ][4 ]int {{1 , 2 , 3 , 4 }} fmt.Println("b = " , b) c := [3 ][4 ]int {2 : {1 , 2 , 3 : 100 }} fmt.Println("c = " , c) }
数组支持比较 只支持:==,!=
同类型且同长度的数组可以赋值 1 2 3 4 a := [5 ]int {2 , 3 , 3 } var b [5 ]int b = a fmt.Println("b = " , b)
随机数的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package mainimport ( "fmt" "math/rand" "time" ) func main () { rand.Seed(time.Now().UnixNano()) for i := 0 ; i < 5 ; i++ { fmt.Println("rand = " , rand.Intn(100 )) fmt.Println("rand = " , rand.Int()) } }
冒泡排序代码的实现 1 2 3 4 5 6 7 8 9 10 11 12 13 func main () { a := []int {10 , 12 , 34 , 330 , 56 , 7 , 2 , 23 } fmt.Println(a) for i := 0 ; i < len (a)-1 ; i++ { for j := 0 ; j < len (a)-i-1 ; j++ { if a[j] > a[j+1 ] { a[j], a[j+1 ] = a[j+1 ], a[j] fmt.Println("a = " , a) } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package mainimport ( "fmt" "math/rand" "time" ) func main () { rand.Seed(time.Now().UnixNano()) var a [10 ]int for i := 0 ; i < len (a); i++ { a[i] = rand.Intn(100 ) } fmt.Println(a) for i := 0 ; i < len (a)-1 ; i++ { for j := i + 1 ; j < len (a); j++ { if a[i] < a[j] { a[i], a[j] = a[j], a[i] fmt.Println(a) } } } fmt.Println(a) }
数组作函数参数 数组作为函数参数,是值拷贝
1 2 3 4 5 6 7 8 9 10 11 func modify (a [5]int ) { a[0 ] = 666 fmt.Println("modify: a= " , a) } func main () { var a [5 ]int = [5 ]int {11 , 12 , 13 , 14 } fmt.Println("main:a = " , a) modify(a) fmt.Println("main:a = " , a) }
数组指针作函数参数 1 2 3 4 5 6 7 8 9 10 11 12 13 func modify_pointer (p *[5]int ) { (*p)[0 ] = 888 fmt.Println("modify_pointer: p= " , *p) } func main () { var a [5 ]int = [5 ]int {11 , 12 , 13 , 14 } fmt.Println("main:a = " , a) modify_pointer(&a) fmt.Println("main modify_pointer:a = " , a) }
数组与切片的区别 数组[]是是固定的一个常量,数组不能修改长度(len)和容量(cap) 切片[]中为空时,或者为…时,切片的长度(len)或容量(cap)可以不固定
1 2 3 4 5 6 7 8 9 10 11 12 func main () { a := [5 ]int {11 , 12 , 13 , 14 } fmt.Printf("a:len(a)=%d,cap(a)=%d\n" , len (a), cap (a)) s := []int {23 , 2 , 5 , 6 } fmt.Printf("s:len(s)=%d,cap(s)=%d\n" , len (s), cap (s)) s = append (s, 1000 ) fmt.Printf("s:s=%v\n" , s) fmt.Printf("s:len(s)=%d,cap(s)=%d\n" , len (s), cap (s)) }
切片的创建 1 2 3 4 5 6 7 8 9 10 11 12 13 func main () { s1 := []int {1 , 2 , 3 } fmt.Println("s1=" , s1) s2 := make ([]int , 5 , 10 ) fmt.Printf("len=%d,cap = %d\n" , len (s2), cap (s2)) s3 := make ([]int , 5 ) fmt.Printf("len=%d,cap = %d\n" , len (s3), cap (s3)) }
切片截取 1 2 3 4 5 6 7 8 9 10 11 12 func main () { s1 := []int {0 , 1 , 2 , 3 , 4 , 5 } fmt.Println("s[n]: s1[1] = " , s1[1 ]) fmt.Println("s1[:]: s1[:] = " , s1[:]) fmt.Println("s1[low:]: s1[1:] = " , s1[1 :]) fmt.Println("s1[:high]: s1[:3] = " , s1[:3 ]) fmt.Println("s1[low:high]: s1[2:4] = " , s1[2 :4 ]) fmt.Println("s1[low:high:max]: s1[2:4:5] = " , s1[2 :4 :5 ]) }
不管切片如何操作,最终都是操作的底层数组1 2 3 4 5 6 7 8 9 func main () { s := []int {0 , 1 , 2 , 3 , 4 , 5 } fmt.Println("s = " , s) s1 := s[2 :4 ] fmt.Println("s[2:4] = s1 = " , s1) s1[0 ] = 666 fmt.Println("s1 = " , s1) fmt.Println("s = " , s) }
append函数的使用 1 2 3 4 var s [] int s = append (s,1 ) s = append (s,2 ,3 ,4 )
copy函数的使用 1 2 3 4 5 6 func main () { src_slice := []int {1 , 2 } dst_slice := []int {6 , 6 , 6 , 6 , 6 } copy (dst_slice, src_slice) fmt.Println(dst_slice) }
切片作函数参数 切片作函数参数,传递的是引用,函数中对传入的变量的修改会作用于函数之外的变量值
1 2 3 4 5 6 7 8 9 func slice_args (s []int ) { s[0 ] = 666 } func main () { s := make ([]int , 1 ) slice_args(s) fmt.Println(s) }
取出数字的每一位 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package mainimport ( "fmt" "math/rand" "time" ) func GetNum (s []int , num int ) { s[0 ] = num / 1000 s[1 ] = num % 1000 / 100 s[2 ] = num % 1000 % 100 / 10 s[3 ] = num % 1000 % 100 % 10 } func main () { rand.Seed(time.Now().UnixNano()) num := rand.Intn(1000 ) fmt.Println("main:num =" , num) s := make ([]int , 4 ) GetNum(s, num) fmt.Println("s=" , s) }
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 rootid@126.com