当前位置:网站首页>Arrays and slices
Arrays and slices
2022-08-09 03:05:00 【laugh addiction】
一、数组
1、数组的定义:
same unique type、长度固定、连续的数据
定义了数组 balance ⻓度为 5 类型为 float32
var balance = [5]float32{
1000.0, 2.0, 3.4, 7.0, 50.0}
balance := [5]float32{
1000.0, 2.0, 3.4, 7.0, 50.0}
If array is set⻓度,我们还可以通过指定下标来初始化元素,将索引为 1 和 3 的元素初始化
balance := [5]float32{
1:2.0,3:7.0}
2、数组的取值:
Values can be obtained by combining subscripts
variable_name[n]
3、二维数组
定义了数组 balance ⻓度为 10 *10类型为 float32
var balance [10][10]float32
balance[0][0] = 100.0
fmt.Println(&balance[0], &balance[1][0], &balance[2])
fmt.Println(balance)
二、切片
1、切片的定义:相同唯一类型、长度是不固定的
2、切片的初始化
1.Initialize the cut directly⽚,[] means cut⽚类型,{
1,2,3} 初始化值依次是 1,2,3,其 cap=len=3.
s :=[] int {
1,2,3 }
2.Initialize cut⽚ s,是数组 arr 的引⽤.
s := arr[:]
3.将 arr 中从下标 startIndex 到 endIndex-1 The elements below are created as⼀a new cut⽚.
s := arr[startIndex:endIndex]
4.默认 endIndex will be indicated⼀直到arr的最后⼀个元素.
s := arr[startIndex:]
5.默认 startIndex will be indicated从 arr 的第⼀个元素开始. [start,end)
s := arr[:endIndex]
6.通过切⽚ s Initialize cut⽚ s1.
s1 := s[startIndex:endIndex]
7.通过内置函数 make() Initialize cut⽚s,[]int 标识为其元素类型为 int 的切⽚.
s :=make([]int,len,cap)
3、len() 和 cap() 函数
切⽚是可索引的,并且可以由 len() ⽅法获取⻓度.
切⽚Provides computing capacity⽅法 cap() Cut can be measured⽚最⻓可以达到多少.
package main
import "fmt"
func main() {
var numbers = make([]int,3,5)
printSlice(numbers)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v**\n**",len(x),cap(x),x)
}
//The following examples are shipped⾏输出结果为:
len=3 cap=5 slice=[0 0 0]
4、append() 和 copy() 函数
append():追加元素
copy(slice1,slice2):将slice2拷贝到slice1里面去
package main
import "fmt"
func main() {
var numbers []int
printSlice(numbers)
/* Additional empty cuts are allowed⽚ */
numbers = append(numbers, 0)
printSlice(numbers)
/* 向切⽚添加⼀个元素 */
numbers = append(numbers, 1)
printSlice(numbers)
/* 同时添加多个元素 */
numbers = append(numbers, 2,3,4)
printSlice(numbers)
/* Create cut⽚ numbers1 was cut before⽚twice the capacity*/
numbers1 := make([]int, len(numbers), (cap(numbers))*2)
/* 拷⻉ numbers 的内容到 numbers1 */
copy(numbers1,numbers)
printSlice(numbers1)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v**\n**",len(x),cap(x),x)
}
//The following code executes⾏输出结果为:
len=0 cap=0 slice=[]
len=1 cap=1 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=6 slice=[0 1 2 3 4]
len=5 cap=12 slice=[0 1 2 3 4]
5、切片和数组的区别
切片:引用(地址相同)
在函数中修改值,Affects external functions
数据拷贝的时候,拷贝的是引用,也就是深拷贝
数组:值
在函数中修改值,不Affects external functions
数据拷贝的时候,只拷贝值,另一个数组不受影响
边栏推荐
- LeetCode_43_字符串相乘
- JavsScript系列-Promise的错误捕获
- 下秒数据CEO蔡致暖受邀参加联合数据举办《数据要素加速跑》线上沙龙
- Recently, I have seen a lot of people who want to study by themselves or enroll in classes but don’t know how to choose. I will tell you about it today.
- ReentrantLock源码分析
- Redis expiration strategy and elimination strategy
- pytorch 自定义dataset
- SQL注入(2)
- 23 Lectures on Disassembly of Multi-merchant Mall System Functions-Platform Distribution Level
- SwiftUI * Grid
猜你喜欢
随机推荐
一本通1258——数字金字塔(动态规划)
JSON beautification plugin for Chrome
Lottie进阶和原理分析
How to deal with cyber attacks?
Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺用于科研实验
通过kvm创建共享磁盘
Redis中SDS简单动态字符串
Promoting practice with competitions-Like the 84th biweekly game reflection and the 305th weekly game supplementary questions
多御安全浏览安卓版升级尝鲜,新增下载管理功能
全文翻译:Multimodal Neural Networks: RGB-D for Segmantic Segmentation and Object Detection
ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题
Redis expiration strategy and elimination strategy
浅聊一下那些营销工具—优惠券
SwiftUI * Grid
Zabbix 5.0 监控教程(四)
dice和iou
20220530设计问题:常数时间插入、删除和获取随机元素
MES对接Simba实现展讯平台 IMEI 写号与耦合测试
数学基础(三)PCA原理与推导
MySQL相关知识 和 数据的存储相关知识