golang basic composite type—slice

Slice overview

Simply put, a slice can be understood as a dynamic array, the length is not fixed, but it is not an array .

Creation of slices

  1. Use var
var name []Type
//例如
var s []int //[]中不需要指定特定长度

//输出
[]
  1. automatic derivation
name := []Type{
    
    }
//例如
s := []int{
    
    1,2,3}
//输出
[1,2,3]
  1. Use make function
name := make([]Type,len,cap) //len是长度,cap是容量,cap可省略,此时len==cap
//例如
s := make([]int,3,5) //len(s)=3,cap(s)=5
//输出
[0,0,0]

Slice initialization

  1. direct initialization
var s []int = []int{
    
    1,2,3}
s1 := []int{
    
    1,2,3,4}
  1. appendadd
s = append(s,4) //此时s = [1,2,3,4]
  1. for loop initialization
s2 := make([]int,3,5)
for i:=0;i<len(s2);i++{
    
    
	s2[i] = i
	}
//s2 = [0,1,2]
  1. Single item assignment and modification
s2[2] = 999
//s2 = [0,1,999]

Slice traversal

  1. for…len method
s:=[]int{
    
    1,2,3}
 for i:=0;i<len(s);i++{
    
    
 		//可以输出每项s[i]
	}

2.for…range method

s:=[]int{
    
    1,2,3}
for index,value := range s{
    
    
	//index 是索引下标,value是索引对应的值
}
//若不需要处理索引或者值可以使用 _ 省略,例如
for _,value := range s{
    
    
	//index 是索引下标,value是索引对应的值
}

Slice interception

s[n]             //切片s中索引位置为n的项
s[:]             //从切片s的索引位置0到len(s)-1处所获得的切片
s[low:]          //从切片s的索引位置low到len(s)-1处所获得的切片
s[:high]        //从切片s的索引位置0到high处(不包含high)所获得的切片m,len = high
s[low:high]     //从切片s的索引位置low到high处(不包含high)所获得的切片,len = high - low
s[low:high:max] //从切片s的索引位置low到high处(不包含high)所获得的切片,len = high - low ,cap = max-low,max 切片中最多能够容纳多少元素
//s[low:high:max] 演示
s := []int{
    
    1,2,3,4,5,6}
s1 := s[0:3:5]
//输出
//s1 = [1,2,3] 不包含下标为3的值 0 1 2
//len(s1) = 3
//cap(s1) = 5-0 = 5

The general calculation formula for capacity cap is cap = max - low

Modifying the value of the intercepted slice will affect the content of the original slice

s := []int{
    
    1,2,3,4,5,6}
s1 := s[0:3:5]  //s1并不是开辟了新的空间而是指向了原切片s
s1[0] = 999
//输出
s1 = [999,2,3]
s = [999,2,3,4,5,6]

Modifying the value of the intercepted slice will affect the content of the original slice

Basic usage of append function

The append function adds elements to the end of the slice

s := make([]int,5,8)
//s = [0,0,0,0,0] 
s = append(s,1)
//s = [0,0,0,0,0,1]
s = append(s,2)
s = append(s,3)
//s = [0,0,0,0,0,1,2,3]  len(s) = 8  cap = 8 已到达切片定义时的容量
s = append(s,4)
//s = [0,0,0,0,0,1,2,3,4] len(s) = 9  cap = 16
//当追加的元素超过定义切片的容量时,会自动扩容,一般扩容方式为上一下cap的两倍,即cap*2
//如果超过1024字节,每次扩容上一次的大约1/4.即约cap*(1+1/4)

Use of copy function

//copy(切片1,切片2)
s1 := []int{
    
    1,2}
s2 := []int{
    
    3,4,5,6,7}
copy(s1,s2)
//s1 = [3,4]

Insert image description here

//copy(切片1,切片2)
s1 := []int{
    
    1,2}
s2 := []int{
    
    3,4,5,6,7}
copy(s2,s1)
//s2 = [1,2,5,6,7]

Insert image description here

Slices as function parameters

func main(){
    
    
	s := []int{
    
    1,2,3,4,5}
	init(s)
}
func init(num []int){
    
    
	for _,v := range num {
    
    
		fmt.printf("%d ",v)
	}
}
//输出
1 2 3 4 5

Note: Modifying the value of the slice in the function will affect the original slice, but the array will not .

func main(){
    
    
	s := []int{
    
    1,2,3,4,5}
	init(s)
	fmt.println()
	fmt.println(s)
}
func init(num []int){
    
    
	for _,v := range num {
    
    
		fmt.printf("%d ",v)
	}
	num[0] = 999
}
//输出
1 2 3 4 5

[999,2,3,4,5]

Slicing Case 1—Calculate the sum of a specific number of integer data

package main

import "fmt"

func main()  {
    
    
	var count int
	fmt.Println("请输入计算整数和的数量:")
	fmt.Scan(&count)
	num := make([]int,count)
	fmt.Println("请输入",count,"个整数:")
	for i:=0;i<len(num);i++{
    
    
		fmt.Scan(&num[i])
	}
	result := sum(num)
	fmt.Println("整数和为:",result)
}
func sum(num []int) int {
    
    
	result := 0
	for _,v := range num{
    
    
		result = result+v
	}
	return result
}

Insert image description here

Slicing case 2—Output the maximum value in an integer array of variable length

package main
import "fmt"

func main()  {
    
    
	var count int
	fmt.Println("请输入计算最大值的整数数量:")
	fmt.Scan(&count)
	num := make([]int,count)
	fmt.Println("请输入",count,"个整数:")
	for i:=0;i<len(num);i++{
    
    
		fmt.Scan(&num[i])
	}
	result := PrintMax(num)
	fmt.Println("最大整数为:",result)
}
func PrintMax(num []int) int {
    
    
	result := 0
	for _,v := range num{
    
    
		if v > result {
    
    
			result = v
		}
	}
	return result
}

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41551445/article/details/126279419