• 第三章、Go-内建容器


    3.1.数组

    (1)数组的定义

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    	//用var定义数组可以不用赋初值
    	var arr1 [5]int                  //[0 0 0 0 0]
    	//用冒号必须指定初值
    	arr2 := [3]int{1,3,5}            //[1 3 5]
    	//用“...”表示任意个数
    	arr3 := [...]int{2,4,6,8,10}     //[2 4 6 8 10]
    	fmt.Println(arr1,arr2,arr3)
    }
    

    (2)数组的遍历

    用range关键字

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    	arr3 := [...]int{2,4,6,8,10}     //[2 4 6 8 10]
    	fmt.Println(arr3)
    
    	//数组的遍历
    	for i := range arr3{
    		fmt.Println(arr3[i])
    	}
    }
    
    
    //结果
    2
    4
    6
    8
    10
    

    获取下标和值

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    	arr3 := [...]int{2,4,6,8,10}     //[2 4 6 8 10]
    	fmt.Println(arr3)
    
    	//数组的遍历
    	for i ,v:= range arr3{
    		fmt.Println(i,v)
    	}
    }
    
    //结果
    0 2
    1 4
    2 6
    3 8
    4 10
    

    (3)数组是值类型

    • [10]int和[20]int是不同类型
    • 调用func f(arr [10]int) 会拷贝数组
    • 在go语言中一般不使用数组,而使用切片
    package main
    
    import (
    	"fmt"
    )
    
    func printArray(arr [5]int){
    	for i ,v:= range arr{
    		fmt.Println(i,v)
    	}
    }
    
    func main() {
    	//arr2 := [3]int{1,3,5}
    	arr3 := [...]int{2,4,6,8,10}    
    	printArray(arr3)
    }
    
    
    //结果
    0 2
    1 4
    2 6
    3 8
    4 10
    

     如果传arr2则报错

    package main
    
    import (
    	"fmt"
    )
    
    func printArray(arr [5]int){
    	for i ,v:= range arr{
    		fmt.Println(i,v)
    	}
    }
    
    func main() {
    	arr2 := [3]int{1,3,5}
    	//arr3 := [...]int{2,4,6,8,10}
    	printArray(arr2)     //cannot use arr2 (type [3]int) as type [5]int in argument to printArray
    }
    

    3.2.切片

    (1)切片

    package main
    
    import "fmt"
    
    func main() {
    	arr := [...]int{0,1,2,3,4,5,6,7}
    	fmt.Println(arr[2:6])	//[2 3 4 5]
    	fmt.Println(arr[:6])	//[0 1 2 3 4 5]
    	fmt.Println(arr[2:])	//[2 3 4 5 6 7]
    	fmt.Println(arr[:])	    //[0 1 2 3 4 5 6 7]
    }
    

    (2)slice的扩展

    slice可以向后扩展,不可以向前扩展

    package main
    
    import "fmt"
    
    func main() {
    	arr := [...]int{0,1,2,3,4,5,6,7}
    	s1 := arr[2:6]
    	s2 := s1[3:5]
    	fmt.Println(s1)     //[2 3 4 5]
    	fmt.Println(s2)    //[5 6]
    }
    

     s2是如何取到[5,6]的

     

     (3)slice的实现

     slice底层隐藏了三个值

    s[i]不可以超越len(s),向后扩展不可以超越底层数组cap(s)

    package main
    
    import "fmt"
    
    func main() {
    	arr := [...]int{0,1,2,3,4,5,6,7}
    	s1 := arr[2:6]
    	s2 := s1[3:5]
    	fmt.Println("arr = ",arr)                                         //arr =  [0 1 2 3 4 5 6 7]
    	fmt.Printf("s1=%v,len(s1)=%d,cap(s1)=%d
    ",s1,len(s1),cap(s1))   //s1=[2 3 4 5],len(s1)=4,cap(s1)=6
    	fmt.Printf("s2=%v,len(s2)=%d,cap(s2)=%d
    ",s2,len(s2),cap(s2))   //s2=[5 6],len(s2)=2,cap(s2)=3
    	fmt.Println(s1[3:6])                                             //[5 6 7]
    }
    

    3.3.切片的操作

    (1)向slice添加元素

    • 添加元素时,如果超出了cap,系统会重新分配更大的底层数组
    • 由于值传递的关系,必须接收append的返回值。s = append(s,val)
    package main
    
    import "fmt"
    
    func main() {
    	arr := [...]int{0,1,2,3,4,5,6,7}
    	s1 := arr[2:6]         //[2 3 4 5]
    	s2 := s1[3:5]          //[5 6]
    	s3 := append(s2,10)
    	s4 := append(s3,11)
    	s5 := append(s4,12)
    	fmt.Println(s3,s4,s5)    //[5 6 10] [5 6 10 11] [5 6 10 11 12]
    	//因为s2的cap是[5,6,7],s3 append把7替换成10,s4后超出了cap
    	fmt.Println(arr)         //[0 1 2 3 4 5 6 10]
    }
    

    (2)slice的创建

    package main
    
    import "fmt"
    
    func main() {
    	//第一种
    	//Zero value for slice is nil
    	var s1 []int
    	fmt.Println(s1)    //[]
    
    	for i := 0; i < 5 ; i++{
    		s1 = append(s1,i)
    	}
    	fmt.Println(s1)    //[0 1 2 3 4]
    
    	//第二种
    	s2 := []int{2,4,6,8}
    	fmt.Printf("len(s2)=%d,cap(s2)=%d
    ",len(s2),cap(s2))    //len(s2)=4,cap(s2)=4
    
    	//第三种
    	s3 := make([]int,16)
    	fmt.Printf("len(s3)=%d,cap(s3)=%d
    ",len(s3),cap(s3))    //len(s3)=16,cap(s3)=16
    
    	//第四种
    	s4 := make([]int,16,32)
    	fmt.Printf("len(s4)=%d,cap(s4)=%d
    ",len(s4),cap(s4))    //len(s4)=16,cap(s4)=32
    }
    

    (3)slice的删除

    package main
    
    import "fmt"
    
    func main() {
    	s1 := []int{0,1,2,3,4,5,6}
    	fmt.Println(s1)                   //[0 1 2 3 4 5 6]
    	//删除index为3的值
    	s1 = append(s1[:3],s1[4:]...)
    	fmt.Println(s1)                   //[0 1 2 4 5 6]
    }
    

    3.4.Map  

    (1)map的三种建立方式

    package main
    
    import "fmt"
    
    func main() {
    	//第一种
    	m1 := map[string]string{
    		"name":   "derek",
    		"course": "go",
    		"city":   "guangdong",
    	}
    	fmt.Println(m1)         //map[city:guangdong course:go name:derek]
    
    	//第二种
    	m2 := make(map[string]int)
    	fmt.Println(m2)          //map[]
    
    	//第三种
    	var m3 map[string]string
    	fmt.Println(m3)           //map[]
    }
    

    (2)map的遍历

    package main
    
    import "fmt"
    
    func main() {
    	m1 := map[string]string{
    		"name":   "derek",
    		"course": "go",
    		"city":   "guangdong",
    	}
    	fmt.Println(m1)         //map[city:guangdong course:go name:derek]
    
    	for k,v := range m1{
    		fmt.Println(k,v)
    	}
    }
    
    //结果
    name derek
    course go
    city guangdong
    

    (3)值的获取

    key存在获取,不存在则得到空值

    package main
    
    import "fmt"
    
    func main() {
    	m1 := map[string]string{
    		"name":   "derek",
    		"course": "go",
    		"city":   "guangdong",
    	}
    	fmt.Println(m1)         //map[city:guangdong course:go name:derek]
    
    	//获取map中key的value
    	courseName := m1["course"]
    	fmt.Println(courseName)    //go
    
    	//如果key不存在,不会报错,而是得到一个空值
    	age := m1["age"]
    	fmt.Println(age)    //空值
    }
    

    (4)用if判断取值

    package main
    
    import "fmt"
    
    func main() {
    	m1 := map[string]string{
    		"name":   "derek",
    		"course": "go",
    		"city":   "guangdong",
    	}
    	fmt.Println(m1)         //map[city:guangdong course:go name:derek]
    
    	//key存在,ok = true
    	courseName,ok := m1["course"]
    	fmt.Println(courseName, ok)    //go true
    
    	//key不存在,ok = false
    	age, ok := m1["age"]     // ok = false
    	fmt.Println(age,ok)
    
    	//取值的时候加一个判断,ok=true取值,否则打印值不存在
    	if age, ok := m1["age"]; ok{
    		fmt.Println(age)
    	}else {
    		fmt.Println("key does not exist")
    	}
    }
    

    (5)删除key

    package main
    
    import "fmt"
    
    func main() {
    	m1 := map[string]string{
    		"name":   "derek",
    		"course": "go",
    		"city":   "guangdong",
    	}
    	fmt.Println(m1)         //map[city:guangdong course:go name:derek]
    	
    	//删除key
    	delete(m1,"city")
    	fmt.Println(m1)         //map[course:go name:derek]
    
    }
    

      

  • 相关阅读:
    2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence
    2015 Multi-University Training Contest 2 hdu 5308 I Wanna Become A 24-Point Master
    2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples
    2015 Multi-University Training Contest 1 y sequence
    BNUOJ 34982 Beautiful Garden
    2015 Multi-University Training Contest 2 Buildings
    HDU 4725 The Shortest Path in Nya Graph
    2015 Multi-University Training Contest 2 Friends
    HDU 4370 0 or 1
    POJ 1847 Tram
  • 原文地址:https://www.cnblogs.com/derek1184405959/p/11285729.html
Copyright © 2020-2023  润新知