当前位置:网站首页>学习 Go 语言 0x02:对切片 Slice 的理解
学习 Go 语言 0x02:对切片 Slice 的理解
2022-04-23 11:02:00 【爱博客大伯】
学习 Go 语言 0x02:对切片 Slice 的理解
只是我自己的理解,仅供参考。如果理解的不对,请不吝指正。谢谢:)
* 例1
代码源自 Go 语言之旅,可能会稍有改动。
package main
import "fmt"
func main() {
s := []int{
2, 3, 5, 7, 11, 13}
fmt.Println(len(s), cap(s),s)
s = s[1:4]
fmt.Println(len(s), cap(s),s)
s = s[:2]
fmt.Println(len(s), cap(s),s)
s = s[1:]
fmt.Println(len(s), cap(s),s)
}
输出:
6 6 [2 3 5 7 11 13]
3 5 [3 5 7]
2 5 [3 5]
1 4 [5]
对输出的解释:
len(s)
是 s
中元素的实际个数。
cap(s)
是切片的容量, 是从 s
的第一个元素开始(从左边界的下标开始),到原数组中最后一个元素的个数。
-
6 6 [2 3 5 7 11 13]
原始s
为[2 3 5 7 11 13]
,容量是6, 从下标0到5。 -
3 5 [3 5 7]
s[1:4]
是从[2 3 5 7 11 13]
中取值,s[1:4]
的值是[3 5 7]
。实际元素个数为3,s[1:4]
中第一个元素是3,3在原数组中的位置是下标1,从下标1到原数组中的下标5,一共是5个位置,所以容量是5。
也可以这样理解:容量是从左边界(下标1)开始,到原数组中的下标5,所以容量是5。 -
2 5 [3 5]
s[:2]
是从[3 5 7]
中取值,所以,s[:2]
的值为[3 5]
。s[:2]
的第一个元素是3,在原数组中下标为1,到原数组下标5,所以容量是5 。
也可以这样理解:s[:2]
并没有改变左边界,还是上一次的左边界(下标1),所以容量还是5。 -
1 4 [5]
s[1:]
是从[3 5]
中取值,所以s[1:]
的值为[5]
。数字5在原数组中的下标为2,到原数组中的下标5,所以容量为4 。
也可以这样理解,s[1:]
又改变了左边界,累积改变了2次左边界,每次左边界为1,都是去掉了一个数,一共去掉了2个数,所以下标从2开始,直到原数组的下标5,容量为4 。
* 例2
package main
import "fmt"
func main() {
s := []int{
2, 3, 5, 7, 11, 13}
printSlice(s)
// Slice the slice to give it zero length.
s = s[:0]
printSlice(s)
// Extend its length.
s = s[:5]
printSlice(s)
// Drop its first two values.
s = s[2:]
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
输出:
len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=5 cap=6 [2 3 5 7 11]
len=3 cap=4 [5 7 11]
对输出的理解:
-
len=6 cap=6 [2 3 5 7 11 13]
s的原始值为{2, 3, 5, 7, 11, 13}
,元素个数为6,容量是6 -
len=0 cap=6 []
s[:0]
是从{2, 3, 5, 7, 11, 13}
中取值,但是由于右边界为0,所以s[:0]
的值为[]
,即为空数组。由于s[:0]
没有改变左边界,即还是从下标0开始,直到原数组中的下标5,所以容量为6。 -
len=5 cap=6 [2 3 5 7 11]
s[:5]
是从原数组{2, 3, 5, 7, 11, 13}
取值,从下标0到下标4,所以s[:5]
的值为[2 3 5 7 11]
。s[:5]
没有改变左边界,所以容量还是6 。
s[:5]
之所以能扩充,是因为上次的s的容量是6,扩充到5个元素,没有超出容量。
容量也可以这样理解:s[:5]
的值为[2 3 5 7 11]
。第一个元素2,在原数组中的下标为0,直到原数组中的最后一个元素的下标5,所以容量为6 。 -
len=3 cap=4 [5 7 11]
s[2:]
改变了左边界,从下标2开始,去掉了下标0和1位置的两个数,所以容量减少2,即cap为4 。而s[2:]
是从上一次的[2 3 5 7 11]
取值,所以s[2:]
的值为[5 7 11]
。
也可以这样理解:s[2:]
的值为[5 7 11]
,第一个元素5 在原数组中的下标为 2,直到原数组中的下标5,所以容量为4 。
* 参考
版权声明
本文为[爱博客大伯]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u013553529/article/details/88902451
边栏推荐
- Typora operation skill description (I)
- Gets the current time in character format
- 面向全球市场,PlatoFarm今日登录HUOBI等全球四大平台
- Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
- Special members and magic methods
- SVN的使用:
- Restful、SOAP、RPC、SOA、微服务之间的区别
- SQL server query database deadlock
- 【leetcode】107. Sequence traversal of binary tree II
- Pycharm
猜你喜欢
How can swagger2 custom parameter annotations not be displayed
ID number verification system based on visual structure - Raspberry implementation
Let the LAN group use the remote device
Comparison and practice of prototype design of knowledge service app
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
Microsoft Access database using PHP PDO ODBC sample
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
GO接口使用
An interesting interview question
Visual common drawing (IV) histogram
随机推荐
Let the LAN group use the remote device
How to Ping Baidu development board
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Read integrity monitoring techniques for vision navigation systems
Hikvision face to face summary
解决方案架构师的小锦囊 - 架构图的 5 种类型
Precautions for latex formula
Go interface usage
Comparison and practice of prototype design of knowledge service app
Full stack cross compilation x86 completion process experience sharing
Problems of class in C # and database connection
Microsoft Access database using PHP PDO ODBC sample
【leetcode】107. Sequence traversal of binary tree II
How to quickly download vscode
我的创作纪念日
Derivation and regularization
SQL server query database deadlock
Visual common drawing (I) stacking diagram
JDBC – PreparedStatement – 如何设置 Null 值?
vm设置静态虚拟机