数组索引基本懂了,头痛,感觉切片好难,下次再学一下,我先睡一下了
This commit is contained in:
25
5.数组索引.go
Normal file
25
5.数组索引.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
list_()
|
||||
}
|
||||
|
||||
func list_() {
|
||||
// 数组
|
||||
var nameList [3]string = [3]string{"skimrme", "lukychen", "Qmeimei"}
|
||||
fmt.Println(nameList)
|
||||
|
||||
// 索引
|
||||
// "skimrme" "lukychen" "Qmeimei"
|
||||
// 0 1 2 正向索引
|
||||
// -3 -2 -1 负向索引
|
||||
fmt.Println(nameList[0]) // 正向索引取值
|
||||
fmt.Println(nameList[2])
|
||||
fmt.Println(nameList[len(nameList)-2]) // 负向索引取值 (go语言原则上不支持的骚操作)
|
||||
// 数组可被切换 但是仿佛是自上而下的
|
||||
nameList[len(nameList)-2] = "123"
|
||||
fmt.Println(nameList)
|
||||
|
||||
}
|
||||
58
6.切片.go
Normal file
58
6.切片.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
slice_()
|
||||
}
|
||||
|
||||
func slice_() {
|
||||
var nameList []string
|
||||
nameList = append(nameList, "1")
|
||||
nameList = append(nameList, "2")
|
||||
|
||||
fmt.Println(nameList[0])
|
||||
|
||||
var nameList1 []string
|
||||
fmt.Println(nameList1 == nil)
|
||||
|
||||
// 初始化
|
||||
var nameList2 []string = []string{}
|
||||
/*
|
||||
其他初始化的方法
|
||||
var nameList2 = []string{}
|
||||
nameList2 := []string{}
|
||||
nameList2 = make([string], 0)
|
||||
*/
|
||||
nameList2 = append(nameList2, "1")
|
||||
nameList2 = append(nameList2, "2")
|
||||
|
||||
fmt.Println(nameList2 == nil)
|
||||
|
||||
// make 函数
|
||||
// 格式
|
||||
//make([]type, length, capacity)
|
||||
// 可以创建指定长度,指定容量的切片
|
||||
ageList := make([]int, 3)
|
||||
fmt.Println(ageList)
|
||||
|
||||
// 数组切片
|
||||
array :=[3]int{1, 2, 3}
|
||||
slices := array[:]
|
||||
fmt.Println(slices)
|
||||
fmt.Println(array[0:2])
|
||||
fmt.Println(array[2:])
|
||||
|
||||
// 切片排序
|
||||
var ints = []int{2, 4, 5, 2, 8, 0, 2, 5,}
|
||||
sort.Ints(ints)
|
||||
fmt.Println(ints) // 默认升序
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(ints))) // 降序
|
||||
fmt.Println(ints)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user