golang

Go语言学习之——切片(Slice)

2018-04-10  本文已影响0人  星空_ad64

1、基础(前闭后开)

arr := [...]int {0,1,2,3,4,5,6,7}
​
fmt.Println("This arr[2:6]: ", arr[2:6] ) // output: This arr[2:6]:  [2 3 4 5]
fmt.Println("This arr[:6]: ", arr[:6] ) // output: This arr[:6]:  [0 1 2 3 4 5]
fmt.Println("This arr[2:]: ", arr[2:] ) // output: This arr[2:]:  [2 3 4 5 6 7]
fmt.Println("This arr[:]: ", arr[:] ) // output: This arr[:]:  [0 1 2 3 4 5 6 7]

2、切片本身没有数据,是对底层array的一个view,所以切片作为函数输入,相当于传地址

package main
​
import "fmt"
​
func changeArray( arr []int ){
    arr[0] = 100
}
​
func main() {
    arr := [...]int {0,1,2,3,4,5,6,7}
    s1 := arr[2:6]
    s2 := arr[:]
​
    fmt.Println("Before: ")
    fmt.Println("s1:", s1) // output: s1: [2 3 4 5]
    fmt.Println("s1:", s2) // output: s2: [0 1 2 3 4 5 6 7]
​
    changeArray(s1)
    fmt.Println("After changeArray(s1): ")
    fmt.Println("s1:", s1) // output: s1: [100 3 4 5]
    fmt.Println("arr:", arr) // output: arr: [0 1 100 3 4 5 6 7]
​
    changeArray(s2)
    fmt.Println("After changeArray(s2): ")
    fmt.Println("s1:", s2) // output: s2: [100 1 100 3 4 5 6 7]
    fmt.Println("arr:", arr) // output: arr: [100 1 100 3 4 5 6 7]
}

3、扩展
切片是可以向后扩展的,但不可以向前扩展
s[i]不可以超越len(s),向后扩展不可以超越底层数组cap(s)


4c5cba17-7313-4026-aea0-cd33f42120db.jpg
arr := [...]int {0,1,2,3,4,5,6,7}

s1 := arr[2:6]
s2 := s1[3:6]

fmt.Printf("len(s1)=%d, cap(s1)=%d\n", len(s1), cap(s1)) // output: len(s1)=4, cap(s1)=6
fmt.Println("s1: ", s1) // output: s1:  [2 3 4 5]
fmt.Println("s2: ", s2) // output: s2:  [5 6 7] 如果超过cap(s1),会报错
84eb3c87-b50e-476f-a3d9-c5bb5df939fe.jpg

s1 := arr[2:6]时,s[4]和s[5]也存在,但是s[i]这样访问不到(会报错),但s2 := s1[3:6]依旧可以访问到


4、添加元素:append

package main
import "fmt"
func changeArray( arr []int ){
    arr[0] = 100
}
func main() {
    arr := [...]int {0,1,2,3,4,5,6,7}
    fmt.Println("arr: ", arr) // output:arr:  [0 1 2 3 4 5 6 7]
    s1 := arr[2:6]
    s2 := s1[3:5]
    fmt.Println("s1: ", s1) // output:s1:  [2 3 4 5]
    fmt.Println("s2: ", s2) // output:s2:  [5 6]
    s3 := append(s2, 10)
    s4 := append(s3, 11)
    s5 := append(s4,12)
    fmt.Println("s3: ", s3) // output:s3:  [5 6 10]
    fmt.Println("s4: ", s4) // output:s4:  [5 6 10 11]
    fmt.Println("s5: ", s5) // output:s5:  [5 6 10 11 12]
    fmt.Println("arr: ", arr) // output:arr:  [0 1 2 3 4 5 6 10]
}

arr从[0 1 2 3 4 5 6 7]变为[0 1 2 3 4 5 6 10]是因为s3 := append(s2, 10),s2=[5 6],再往后添加10的时候,把arr中的7变为了10。而后面再添加11、12时,因为已经超越了arr的cap,所以系统会重新分配更大的底层数组,而不再是对arr进行操作,原来的数组如果有人用就会依旧存在,如果没人用了就会自动垃圾回收
由于是值传递,所以必须接受append的返回值。


5、创建

package main
import "fmt"
func printSlice(s []int) {
    fmt.Printf("len=%d, cap=%d\n",len(s),cap(s))
}
func main() {
    //方法一:
    var s []int
    for i := 0; i < 20; i++ {
        printSlice(s)
        s = append(s, 2*i+1)
    } // output见下
    fmt.Println(s) // output:[1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39]
    //方法二:
    s1 := []int{2,4,6,8}
    fmt.Println(s1) // output:[2 4 6 8]
    //方法三:
    s2 := make([]int, 16) // 16为指定的len
    fmt.Println(s2) // output:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    printSlice(s2) // output:len=16, cap=16
    s3 := make([]int, 10, 32) // 16为指定的len,32为指定的cap
    fmt.Println(s3) // output:[0 0 0 0 0 0 0 0 0 0]
    printSlice(s3) // output:len=10, cap=32
}

for循环中printSlice(s)的output:
len=0, cap=0
len=1, cap=1
len=2, cap=2
len=3, cap=4
len=4, cap=4
len=5, cap=8
len=6, cap=8
len=7, cap=8
len=8, cap=8
len=9, cap=16
len=10, cap=16
len=11, cap=16
len=12, cap=16
len=13, cap=16
len=14, cap=16
len=15, cap=16
len=16, cap=16
len=17, cap=32
len=18, cap=32
len=19, cap=32
可见,没增加一个len加1,而cap则是每当当前的空间不够时,就扩充一倍


6、拷贝
copy(目的,源)


4ac74bcb-48f6-48e2-ad39-050c98003bde.png
package main
import "fmt"
func main() {
    s1 := []int{2,4,6,8}
    fmt.Println("s1:",s1) // output:s1: [2 4 6 8]
    s2 := make([]int, 16)
    fmt.Println("s2:",s2) // output:s2: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    copy(s2,s1)
    fmt.Println("s2:",s2) // output:s2: [2 4 6 8 0 0 0 0 0 0 0 0 0 0 0 0]
}

7、删除

package main

import "fmt"

func main() {

    s1 := []int{2,4,6,8}
    fmt.Println("s1:",s1) // output:s1: [2 4 6 8]

    s2 := make([]int, 16)
    fmt.Println("s2:",s2) // output:s2: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

    copy(s2,s1)
    fmt.Println("s2:",s2) // output:s2: [2 4 6 8 0 0 0 0 0 0 0 0 0 0 0 0]

    s2 = append(s2[:3], s2[4:]...) // s2 = s2[:3] + s2[4:],去掉了s2[3]
    fmt.Println("s2:",s2) // output:s2: [2 4 6 0 0 0 0 0 0 0 0 0 0 0 0]

    s2 = s2[1:] // 去掉头
    fmt.Println("s2:",s2) // output:s2: [4 6 0 0 0 0 0 0 0 0 0 0 0 0]

    s2 = s2[:len(s2)-1] // 去掉最后一个
    fmt.Println("s2:",s2) // output:s2: [4 6 0 0 0 0 0 0 0 0 0 0 0]
}
上一篇 下一篇

猜你喜欢

热点阅读