golang 8. 数组 切片
数组 array.go
package main
import "fmt"
func printArray(myArray [4]int) {
//值拷贝
for index, value := range myArray {
fmt.Println("index = ", index, ", value = ", value)
}
myArray[0] = 111
}
func main() {
//固定长度的数组
var myArray1 [10]int
myArray2 := [10]int{1,2,3,4}
myArray3 := [4]int{11,22,33,44}
//for i := 0; i < 10; i++ {
for i := 0; i < len(myArray1); i++ {
fmt.Println(myArray1[i])
}
for index, value := range myArray2 {
fmt.Println("index = ", index, ", value = ", value)
}
//查看数组的数据类型
fmt.Printf("myArray1 types = %T\n", myArray1)
fmt.Printf("myArray2 types = %T\n", myArray2)
fmt.Printf("myArray3 types = %T\n", myArray3)
printArray(myArray3)
fmt.Println(" ------ ")
for index, value := range myArray3 {
fmt.Println("index = ", index, ", value = ", value)
}
}
切片
package main
import "fmt"
func printArray(myArray []int) {
//引用传递
// _ 表示匿名的变量
for _, value := range myArray {
fmt.Println("value = ", value)
}
myArray[0] = 100
}
func main() {
myArray := []int{1,2,3,4} // 动态数组,切片 slice
fmt.Printf("myArray type is %T\n", myArray)
printArray(myArray)
fmt.Println(" ==== ")
for _, value := range myArray {
fmt.Println("value = ", value)
}
}
package main
import "fmt"
func main() {
//声明slice1是一个切片,并且初始化,默认值是1,2,3。 长度len是3
//slice1 := []int{1, 2, 3}
//声明slice1是一个切片,但是并没有给slice分配空间
var slice1 []int
//slice1 = make([]int, 3) //开辟3个空间 ,默认值是0
//声明slice1是一个切片,同时给slice分配空间,3个空间,初始化值是0
//var slice1 []int = make([]int, 3)
//声明slice1是一个切片,同时给slice分配空间,3个空间,初始化值是0, 通过:=推导出slice是一个切片
//slice1 := make([]int, 3)
fmt.Printf("len = %d, slice = %v\n", len(slice1), slice1)
//判断一个silce是否为0
if slice1 == nil {
fmt.Println("slice1 是一个空切片")
} else {
fmt.Println("slice1 是有空间的")
}
}
package main
import "fmt"
func main() {
var numbers = make([]int, 3, 5)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers), cap(numbers), numbers)
//向numbers切片追加一个元素1, numbers len = 4, [0,0,0,1], cap = 5
numbers = append(numbers, 1)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers), cap(numbers), numbers)
//向numbers切片追加一个元素2, numbers len = 5, [0,0,0,1,2], cap = 5
numbers = append(numbers, 2)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers), cap(numbers), numbers)
//向一个容量cap已经满的slice 追加元素,
numbers = append(numbers, 3)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers), cap(numbers), numbers)
fmt.Println("-=-------")
var numbers2 = make([]int, 3)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers2), cap(numbers2), numbers2)
numbers2 = append(numbers2, 1)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers2), cap(numbers2), numbers2)
numbers2 = append(numbers2,2)
numbers2 = append(numbers2,3)
numbers2 = append(numbers2,4)
fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers2), cap(numbers2), numbers2)
// cap * 2
/*
len = 3, cap = 5, slice = [0 0 0]
len = 4, cap = 5, slice = [0 0 0 1]
len = 5, cap = 5, slice = [0 0 0 1 2]
len = 6, cap = 10, slice = [0 0 0 1 2 3]
-=-------
len = 3, cap = 3, slice = [0 0 0]
len = 4, cap = 6, slice = [0 0 0 1]
len = 7, cap = 12, slice = [0 0 0 1 2 3 4]
*/
}
package main
import "fmt"
func main() {
s := []int{1, 2, 3} //len = 3, cap = 3, [1,2,3]
//[0, 2)
s1 := s[0:2] // [1, 2]
fmt.Println(s1)
s1[0] = 100
fmt.Println(s)
fmt.Println(s1)
//copy 可以将底层数组的slice一起进行拷贝
s2 := make([]int, 3) //s2 = [0,0,0]
//将s中的值 依次拷贝到s2中
copy(s2, s)
fmt.Println(s2)
/*
[1 2]
[100 2 3]
[100 2]
[100 2 3]
*/
}