zoukankan      html  css  js  c++  java
  • Go中的数组切片的使用总结

    代码示例

    package main
    
    import "fmt"
    
    func main(){
    	fmt.Println("Hello, world")
    
    	// 定义数组的常规方法
    	/******************************
    	[32]byte    // 长度为32的数组
    	[2 * N] struct {x, y int32}    // 复杂类型数组
    	[100]*float32    // 指针数组
    	[2][3]int     // 二维数组
    	[2][2][2] float64     // 等同于[2] ([2] ([2] float64))
    	******************************/
    	array := [5]int{1,2,3,4,5}
    
    	// 遍历数组
    	fmt.Println("Traverse func 1 --->")
    	for i:= 0; i < len(array); i++ {
    		fmt.Printf("%d	",array[i])
    	}
    	fmt.Println()
    
    	for i, v := range array {
    		fmt.Printf("index %d is %d
    ", i, v)
    	}
    
    	// try to use modify func to modify array
    	// cannot success, value type
    	modifyArray(array)
    	fmt.Println("In main func(), array values -->", array)
    
    	// use slice 
    	useSlice()
    }
    
    // 数组是值类型
    func modifyArray(array [5]int){
    	array[0] = 12
    	fmt.Println("In Modify func(), array values -->", array)
    }
    
    
    // 数组切片的结构定义
    	// 一个指向原生数组的指针
    	// 数据切片的元素个数
    	// 数组切片已分配的存储空间
    func useSlice(){
    
    	// 1. 创建数组切片
    	// 基于数组
    	var myArr [3]int32 = [3]int32{23,1,3}
    	// myArray[first:last]
    	var mySlice []int32 = myArr[:]
    
    	fmt.Println("Elements of myArr is")
    	for _, v := range myArr {
    		fmt.Printf("%d	", v)
    	}
    	fmt.Println()
    
    	fmt.Println("
    Element of mySlice is ")
    	for _, v := range mySlice {
    		fmt.Printf("%d	", v)
    	}
    	fmt.Println()
    
    	// 2. 直接创建
    	fmt.Println("直接创建数组切片")
    	// 创建一初始元素个数为5,初始值为0,预留10个元素的存储空间的slice
    	mySliceOne := make([]int, 5, 10)
    	fmt.Println("
    mySliceOne is ", mySliceOne)
    
    	// 创建并且初始化包含5个元素的数组切片
    	mySliceTwo := []int{1,2,3,43,23}
    	fmt.Println("
    mySliceTwo is ", mySliceTwo)
    
    	// 3. 遍历数组切片
    	for _, v := range mySliceTwo {
    		fmt.Printf("%d	", v)
    	}
    
    	// 4. 动态增减元素
    	// go语言内置cap和len两个函数
    	// cap返回的是数组切片的分配的空间大小,len返回的屙屎数组切片中当前所存储的元素个数
    	myNewSlice := make([]int, 5, 10)
    	fmt.Println("myNewSlice is ", myNewSlice)
    	fmt.Println("len(myNewSlice) is ", len(myNewSlice))
    	fmt.Println("cap(myNewSlice) is ",cap(myNewSlice))
    
    	// 5. 如果需要继续在myNewSlice中添加3个元素,使用append方法
    	myNewSlice = append(myNewSlice, 12, 12 , 23)
    	fmt.Println("after append 5 elements in slice, results is ", myNewSlice)
    
    	// append后面的参数其实是不定参数, 也可以直接将一个slice添加到一个slice中,只是写法会有些不一样
    	subSlice := []int{12,2,1}
    	// 注意第二个参数后面的三个点
    	// 数组切片会自动处理内存空间不足的问题,如果内存不够,就会自动分配一块够大的空间
    	myNewSlice = append(myNewSlice, subSlice...)
    	fmt.Println("after append other slice in slice, results is ", myNewSlice)
    
    	// 6. 基于数组切片创建数组切片
    	oldSlice := []int{12,23,12}
    	newSlice := oldSlice[1:2]
    	fmt.Println("newSlice is ", newSlice)
    
    	// 7. 内容复制
    	// copy内置函数,如果两个数组切片不一样大,则按照其中个数少的那个进行复制
    	s1 := []int{1,2,34,4,5,6}
    	s2 := []int{4,21,1}
    	
    	// 只复制s1的前3个元素到s2中
    	fmt.Println("
    s2 is ", s2)
    	copy(s2, s1)
    	fmt.Println("copy elements of s1 to s2 is ", s2)
    
    	// 只复制s2的3个元素到s2的前3个元素中
    	fmt.Println("
    s1 is ", s1)
    	copy(s1, s2)
    	fmt.Println("copy elements of s2 to s1 is ", s1)
    
    }
    

    输出结果

    Hello, world
    Traverse func 1 --->
    1       2       3       4       5
    index 0 is 1
    index 1 is 2
    index 2 is 3
    index 3 is 4
    index 4 is 5
    In Modify func(), array values --> [12 2 3 4 5]
    In main func(), array values --> [1 2 3 4 5]
    Elements of myArr is
    23      1       3
    
    Element of mySlice is
    23      1       3
    直接创建数组切片
    
    mySliceOne is  [0 0 0 0 0]
    
    mySliceTwo is  [1 2 3 43 23]
    1       2       3       43      23      myNewSlice is  [0 0 0 0 0]
    len(myNewSlice) is  5
    cap(myNewSlice) is  10
    after append 5 elements in slice, results is  [0 0 0 0 0 12 12 23]
    after append other slice in slice, results is  [0 0 0 0 0 12 12 23 12 2 1]
    newSlice is  [23]
    
    s2 is  [4 21 1]
    copy elements of s1 to s2 is  [1 2 34]
    
    s1 is  [1 2 34 4 5 6]
    copy elements of s2 to s1 is  [1 2 34 4 5 6]
    
  • 相关阅读:
    AtCoder Regular Contest 093
    AtCoder Regular Contest 094
    G. Gangsters in Central City
    HGOI 20190711 题解
    HGOI20190710 题解
    HGOI 20190709 题解
    HGOI 20190708 题解
    HGOI20190707 题解
    HGOI20190706 题解
    HGOI 20190705 题解
  • 原文地址:https://www.cnblogs.com/zuixime0515/p/12846834.html
Copyright © 2011-2022 走看看