切片的长度和容量
/*
要点: 切片是一种数据结构
*/
package main
import (
"fmt"
)
func main() {
a := []int{1, 2, 3, 0, 0} //切片初始化
s := a[0:3:5] //从下标0开始
fmt.Println("s = ", s)
fmt.Println("len(s) = ", len(s)) //长度: 3----(3 - 0)
fmt.Println("cap(s) = ", cap(s)) //容量: 5----(5 - 0)
s = a[1:4:5]
fmt.Println("s = ", s)
fmt.Println("len(s) = ", len(s)) //长度: 3----(4 - 1)
fmt.Println("cap(s) = ", cap(s)) //容量: 4----(5 - 1)
}
切片的创建
方式1
/*
要点: 切片和数组的区别
*/
package main
import (
"fmt"
)
func main() {
//数组[]里面的长度是一个固定的常量,数组不能修改长度,len和cap为固定值
a := [5]int{1, 2, 3, 4, 5}
fmt.Printf("len(a) = %d, cap(a) = %d\n", len(a), cap(a))
//切片[]里面为空,切片的长度和容量不固定
s := []int{}
fmt.Printf("len(s) = %d, cap(s) = %d\n", len(s), cap(s))
s = append(s, 1) //切片末尾追加成员
fmt.Printf("len(s) = %d, cap(s) = %d\n", len(s), cap(s))
}
方式2
package main
import (
"fmt"
)
func main() {
//自动推导类型
s := []int{1, 2, 3, 4}
fmt.Println("s = ", s)
//借助make函数 格式: make(type, len, cap)
s1 := make([]int, 5, 10)
fmt.Printf("len(s1) = %d, cap(s1) = %d\n", len(s1), cap(s1))
//没有指定容量 则长度=容量
s2 := make([]int, 5)
fmt.Printf("len(s2) = %d, cap(s2) = %d\n", len(s2), cap(s2))
}
切片的截取
package main
import (
"fmt"
)
func main() {
slice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
//[low:high:max] 取下标从low开始的元素 len = high - low, cap = max - low
s := slice[:]
fmt.Println("s = ", s)
fmt.Printf("len(s) = %d, cap(s) = %d\n", len(s), cap(s))
s1 := slice[3]
fmt.Println("s1 = ", s1) //3
s2 := slice[1:5:8]
fmt.Println("s2 = ", s2)
fmt.Printf("len(s2) = %d, cap(s2) = %d\n", len(s2), cap(s2))
s3 := slice[7:] //cap = 3 等价于slice[7:10:10]
fmt.Println("s3 = ", s3)
fmt.Printf("len(s3) = %d, cap(s3) = %d\n", len(s3), cap(s3))
s4 := slice[:4] //cap = 10 等价于slice[0:4:10]
fmt.Println("s4 = ", s4)
fmt.Printf("len(s4) = %d, cap(s4) = %d\n", len(s4), cap(s4))
s5 := slice[1:4] //cap = 9 等价于slice[1:4:10]
fmt.Println("s5 = ", s5)
fmt.Printf("len(s5) = %d, cap(s5) = %d\n", len(s5), cap(s5))
}
切片与底层数组的关系
package main
import (
"fmt"
)
func main() {
slice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
s := slice[2:5]
fmt.Println("s = ", s)
fmt.Printf("len(s) = %d, cap(s) = %d\n", len(s), cap(s))
s[1] = 10 //会改变底层数组
fmt.Println("s = ", s)
fmt.Println("slice = ", slice)
s1 := s[2:7]
fmt.Println("s1 = ", s1)
s1[2] = 17
fmt.Println("s1 = ", s1)
fmt.Println("slice = ", slice)
}
append函数
package main
import (
"fmt"
)
func main() {
s := []int{}
fmt.Printf("len = %d, cap = %d\n", len(s), cap(s))
fmt.Println("s = ", s)
//原切片末尾添加
s = append(s, 1)
s = append(s, 2)
s = append(s, 3)
fmt.Printf("len = %d, cap = %d\n", len(s), cap(s))
fmt.Println("s = ", s)
s1 := []int{1, 2, 3}
s1 = append(s1, 4)
s1 = append(s1, 5)
s1 = append(s1, 6)
fmt.Println("s1 = ", s1)
}
append函数扩容
/*
重点: 切片是2倍扩容
*/
package main
import (
"fmt"
)
func main() {
//如果超过原来容量,通常以2倍容量扩容
s := make([]int, 0, 1)
oldCap := cap(s)
for i := 0; i < 20; i++ {
s = append(s, i)
if newCap := cap(s); oldCap < newCap {
fmt.Printf("cap:%d ===> %d\n", oldCap, newCap)
oldCap = newCap
}
}
}
内建函数
/*
重点: copy函数的使用
*/
package main
import (
"fmt"
)
func main() {
srcSlice := []int{1, 2, 3, 4, 9, 10}
dstSlice := []int{5, 6, 7, 8}
copy(dstSlice, srcSlice)
fmt.Println("srcSlice = ", srcSlice)
fmt.Println("dstSlice = ", dstSlice) //结果: [1,2,3,4]
srcSlice1 := []int{1, 2}
dstSlice1 := []int{5, 6, 7, 8}
copy(dstSlice1, srcSlice1)
fmt.Println("srcSlice1 = ", srcSlice1)
fmt.Println("dstSlice1 = ", dstSlice1) //结果: [1,2,7,8]
}
切片作函数参数
/*
重点: 切片是引用传递
*/
package main
import (
"fmt"
"math/rand"
"time"
)
func InitData(s []int) {
//设置种子
rand.Seed(time.Now().UnixNano())
for i := 0; i < len(s); i++ {
s[i] = rand.Intn(100) //创建100以内的随机数
}
}
func BubbleSort(s []int) {
n := len(s)
for i := 0; i < n-1; i++ {
for j := 0; j < n-1-i; j++ {
if s[j] > s[j+1] {
s[j], s[j+1] = s[j+1], s[j]
}
}
}
}
func main() {
n := 10
//创建一个切片,len是10
s := make([]int, n)
//初始化数组
InitData(s)
fmt.Println("排序前: ", s)
//冒泡排序
BubbleSort(s)
fmt.Println("排序后: ", s)
}
猜数字游戏
package main
import (
"fmt"
"math/rand"
"time"
)
func CreateNum(p *int) {
//设置种子
rand.Seed(time.Now().UnixNano())
var num int
for {
num = rand.Intn(10000)
if num >= 1000 {
break
}
}
*p = num
}
func GetNum(s []int, num int) {
s[0] = num / 1000 //千位
s[1] = num % 1000 / 100 //百位
s[2] = num % 100 / 10 //十位
s[3] = num % 10 //个位
}
func PlayOneGame(s []int) {
var num int
keySlice := make([]int, 4)
for {
for {
fmt.Printf("请输入一个四位数: ")
fmt.Scan(&num)
if num > 999 && num < 10000 {
break
}
fmt.Printf("输入的数不符合要求,请重新输入\n")
}
GetNum(keySlice, num)
index := 0
for i := 0; i < 4; i++ {
if keySlice[i] > s[i] {
fmt.Printf("第%d位大了一点\n", i+1)
} else if keySlice[i] < s[i] {
fmt.Printf("第%d位小了一点\n", i+1)
} else {
fmt.Printf("第%d位猜对了\n", i+1)
index++
}
}
if index == 4 {
fmt.Println("全部猜对")
break
}
}
}
func main() {
var randNum int
//产生一个4位的随机数
CreateNum(&randNum)
randSlice := make([]int, 4)
//获取切片
GetNum(randSlice, randNum)
//猜切片的值
PlayOneGame(randSlice)
}