自学golang【3.6切片slice的练习代码】切片的长度,上限,复制,删除与增加

切片的练习代码:

package main

import "fmt"

//slice本身没有数据,是对底层array的一个view
func updateslice(s []int) { //使用切片修改数组数据
	s[0] = 100
}
func main() {
	arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
	fmt.Println("arr[2:6]=", arr[2:6])
	fmt.Println("arr[:6]=", arr[:6])
	s1 := arr[2:]
	fmt.Println("arr[2:]=", s1)
	s2 := arr[:]
	fmt.Println("arr[:]=", s2)
	fmt.Println("s1")
	updateslice(s1) //修改s1
	fmt.Println(s1)
	fmt.Println(arr)
	fmt.Println("s2")
	updateslice(s2) //修改s2
	fmt.Println(s2)
	fmt.Println(arr)
	//在切片上再进行切片
	fmt.Println("reslice s2")
	fmt.Println(s2)
	s2 = s2[:5]
	fmt.Println(s2)
	s2 = s2[2:]
	fmt.Println(s2)
	//切片可以向后拓展,但不可以向前拓展
	fmt.Println("extend slice s2") //切片的扩展
	arrr := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
	s11 := arrr[2:6] //2,3,4,5
	s22 := s11[3:5]  //5.但会拓展,实际结果为5,6
	fmt.Println(s11)
	fmt.Println(s22)
	//s[i]不可以超越len(s),向后拓展不可以超越底层数组cap(s)
	fmt.Printf("s1=%v,len(s1)=%d,cap(s1)=%d\n", s11, len(s11), cap(s11))
	fmt.Printf("s2=%v,len(s2)=%d,cap(s2)=%d\n", s22, len(s22), cap(s22))
	//切片的增加
	s3 := append(s22, 10)
	s4 := append(s3, 11)
	s5 := append(s4, 12)
	fmt.Println("s3,s4,s5=", s3, s4, s5)
	fmt.Println("arrr=", arrr)
	//添加元素时如果超越cap,系统会重新分配更大的底层数组
	//由于值传递的关系,必须接收append的返回值
	//s=append(s,val)
}

输出结果:

arr[2:6]= [2 3 4 5]
arr[:6]= [0 1 2 3 4 5]                       
arr[2:]= [2 3 4 5 6 7]                       
arr[:]= [0 1 2 3 4 5 6 7]                    
s1                                           
[100 3 4 5 6 7]                              
[0 1 100 3 4 5 6 7]                          
s2                                           
[100 1 100 3 4 5 6 7]                        
[100 1 100 3 4 5 6 7]                        
reslice s2                                   
[100 1 100 3 4 5 6 7]                        
[100 1 100 3 4]                              
[100 3 4]                                    
extend slice s2                              
[2 3 4 5]                                    
[5 6]                                        
s1=[2 3 4 5],len(s1)=4,cap(s1)=6             
s2=[5 6],len(s2)=2,cap(s2)=3                 
s3,s4,s5= [5 6 10] [5 6 10 11] [5 6 10 11 12]
arrr= [0 1 2 3 4 5 6 10]    

切片的长度,上限,复制,删除与增加

package main

import "fmt"

func printslice(s []int) {
	fmt.Printf("len=%d,cap=%d\n", len(s), cap(s))
}
func main() {
	var s []int //zero value for slice is nil
	for i := 0; i < 100; i++ {
		printslice(s) //随着不断的添加数据其长度和上限都在增加,最初的nil空值系统不会崩溃
		s = append(s, 2*i+1)
	}
	fmt.Println(s)
	//创建一个长度为16的切片先不放值
	s2 := make([]int, 16)
	//创建一个长度为10,上限为32的切片先不放值
	s3 := make([]int, 10, 32)
	printslice(s2)
	printslice(s3)
	//复制切片
	s1 := []int{2, 4, 6, 8} //切片的【】内不用写...
	copy(s2, s1)
	fmt.Println(s2)
	printslice(s2)
	//删除一个切片元素
	s2 = append(s2[:3], s2[4:]...) //使用append函数删除8元素且使后面的数前进
	fmt.Println(s2)
	printslice(s2)
	//删除第一个元素
	front := s2[0]
	s2 = s2[1:]
	fmt.Println(front)
	fmt.Println(s2)
	printslice(s2)
	//删除最后一个元素
	tail := s2[len(s2)-1]
	s2 = s2[:len(s2)-1]
	fmt.Println(tail)
	fmt.Println(s2)
	printslice(s2)
}

结果:

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=20,cap=32
len=21,cap=32
len=22,cap=32
len=23,cap=32
len=24,cap=32
len=25,cap=32
len=26,cap=32
len=27,cap=32
len=28,cap=32
len=29,cap=32
len=30,cap=32
len=31,cap=32
len=32,cap=32
len=33,cap=64
len=34,cap=64
len=35,cap=64
len=36,cap=64
len=37,cap=64
len=38,cap=64
len=39,cap=64
len=40,cap=64
len=41,cap=64
len=42,cap=64
len=43,cap=64
len=44,cap=64
len=45,cap=64
len=46,cap=64
len=47,cap=64
len=48,cap=64
len=49,cap=64
len=50,cap=64
len=51,cap=64
len=52,cap=64
len=53,cap=64
len=54,cap=64
len=55,cap=64
len=56,cap=64
len=57,cap=64
len=58,cap=64
len=59,cap=64
len=60,cap=64
len=61,cap=64
len=62,cap=64
len=63,cap=64
len=64,cap=64
len=65,cap=128
len=66,cap=128
len=67,cap=128
len=68,cap=128
len=69,cap=128
len=70,cap=128
len=71,cap=128
len=72,cap=128
len=73,cap=128
len=74,cap=128
len=75,cap=128
len=76,cap=128
len=77,cap=128
len=78,cap=128
len=79,cap=128
len=80,cap=128
len=81,cap=128
len=82,cap=128
len=83,cap=128
len=84,cap=128
len=85,cap=128
len=86,cap=128
len=87,cap=128
len=88,cap=128
len=89,cap=128
len=90,cap=128
len=91,cap=128
len=92,cap=128
len=93,cap=128
len=94,cap=128
len=95,cap=128
len=96,cap=128
len=97,cap=128
len=98,cap=128
len=99,cap=128
[1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 10
7 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 14
7 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 18
7 189 191 193 195 197 199]
len=16,cap=16
len=10,cap=32
[2 4 6 8 0 0 0 0 0 0 0 0 0 0 0 0]
len=16,cap=16
[2 4 6 0 0 0 0 0 0 0 0 0 0 0 0]
len=15,cap=16
2
[4 6 0 0 0 0 0 0 0 0 0 0 0 0]
len=14,cap=15
0
[4 6 0 0 0 0 0 0 0 0 0 0 0]
len=13,cap=15

运行截图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

立志冲海大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值