大家好,又见面了,我是全栈君。
一数组
数量写在类型前面
可通过_省略变量
不仅range,任何地方都可通过_省略变量
如果只要i下标,可写成 for i:= range numbers
数组是值类型
int[10] 和int[20] 是不同类型
调用func f(arr [10]int)会拷贝数组
在go语言中一般不直接使用数组
二. slice 切片:
s:=[2:6] 半开半闭区间 ,s的值为[2,3,4,5]
package main
import "fmt"
func main() {
arr := [...]int{0,1, 2, 3, 4, 5, 6}
s := arr[2:6]
fmt.Println("arr[2:6]", s) //切片半开半闭区间 arr[2:6] [2 3 4 5]
}
package main
import "fmt"
func updateSlice(s []int) {
s[0] = 100
}
func main() {
arr := [...]int{0, 1, 2, 3, 4, 5, 6,7}
//s := arr[2:6]
//fmt.Println("arr[2:6]", s) //切片半开半闭区间 arr[2:6] [2 3 4 5]
fmt.Println("arr[2:6]", arr[2:6]) // arr[开始下标:结束下标]
fmt.Println("arr[:6]", arr[:6])
s1 := arr[2:]
fmt.Println("s1", arr[2:])
s2 := arr[:]
fmt.Println("s2", arr[:])
fmt.Println("after update slice(s1)")
updateSlice(s1)
fmt.Println(s1)
fmt.Println(arr)
//after update slice
//[100 33 4 5 6]
//[0 1 100 33 4 5 6]
fmt.Println("after update slice(s2)")
updateSlice(s2)
fmt.Println(s2)
fmt.Println(arr)
//after update slice(s2)
//[100 1 100 33 4 5 6]
//[100 1 100 33 4 5 6]
fmt.Println("extending slice")
arr[0],arr[2]=0,2
s1=arr[2:6]
s2=s1[3:5]
fmt.Println("arr:",arr)//arr= [0 1 2 3 4 5 6 7]
fmt.Println("s1:",s1)
fmt.Println("s2:",s2)
fmt.Println("--")
fmt.Println("arr=",arr)
fmt.Println("s1=%v,len(s1)=%d,cap(s1)=%d",s1,len(s1),cap(s1))//s1=%v,len(s1)=%d,cap(s1)=%d [2 3 4 5] 4 6
fmt.Println("s2=%v,len(s2)=%d,cap(s2)=%d",s2,len(s2),cap(s2))//s2=%v,len(s2)=%d,cap(s2)=%d [5 6] 2 3
fmt.Println(s1[3:6])// [5 6 7]
}
//arr[2:6] [2 33 4 5]
//arr[:6] [0 1 2 33 4 5]
//arr[2:] [2 33 4 5 6]
//arr[:] [0 1 2 33 4 5 6]
操作:
添加元素时如果超越cap,系统会重新分配更大的低层数组
由于值传递的关系,必须接受append的返回值
package main
import "fmt"
func printSlice(s []int) {
fmt.Printf("%v,len=%d,cap=%d \n", s, len(s), cap(s))
}
func main() {
fmt.Println("Create slice")
var s []int
for i := 0; i < 100; i++ {
printSlice(s)
s = append(s, 2*i+1)
}
fmt.Println(s)
fmt.Println(len(s))//100
s1 := []int{2, 4, 6, 8} [2 4 6 8],len=4,cap=4
s2 := make([]int, 16)//[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],len=16,cap=16
s3 := make([]int, 10, 32)//[0 0 0 0 0 0 0 0 0 0],len=10,cap=32
printSlice(s1)
printSlice(s2)
printSlice(s3)
fmt.Println("copy slice")
copy(s2, s1)
printSlice(s2)
//copy slice
//[2 4 6 8 0 0 0 0 0 0 0 0 0 0 0 0],len=16,cap=16
fmt.Println("delete elements from slice")
s2 = append(s2[:3], s2[4:]...)
printSlice(s2)
//delete elements from slice
//[2 4 6 0 0 0 0 0 0 0 0 0 0 0 0],len=15,cap=16
fmt.Println("popping from front")
front := s2[0]
s2 = s2[1:]
fmt.Println(front)
printSlice(s2)
//popping from front
//2
//[4 6 0 0 0 0 0 0 0 0 0 0 0 0],len=14,cap=15
fmt.Println("popping from back")
tail := s2[len(s2)-1]
s2 = s2[:len(s2)-1]
fmt.Println(tail)
printSlice(s2)
//popping from back
//0
//[4 6 0 0 0 0 0 0 0 0 0 0 0],len=13,cap=15
}
三.map
1.map的操作
2.map的遍历
3.map的key
package main
import "fmt"
func main() {
//定义map3种方式
m := map[string]string{
"name": "lxw",
"course": "golang",
"site": "imooc",
"quality": "notbad",
}
m2 := make(map[string]int) //m2==empty map
var m3 map[string]int //m3==nil
fmt.Println(m, m2, m3)
fmt.Println("==traversing map==")
for k, v := range m {
fmt.Println(k, v)
}
fmt.Println("---getting values---")
courseName, ok := m["course"]
fmt.Println(courseName, ok)
//判断键是否存在
if casseName, ok2 := m["caurse"]; ok2 {
fmt.Println(casseName)
} else {
fmt.Println("key does not exist")
}
fmt.Println("--deleting values---")
courseName, ok3 := m["name"]
fmt.Println(courseName, ok3)
delete(m, "name")
courseName, ok3 = m["name"]
fmt.Println(courseName, ok3)
}
/*map[course:golang name:lxw quality:notbad site:imooc] map[] map[]
==traversing map==
quality notbad
name lxw
course golang
site imooc
---getting values---
golang true
key does not exist
--deleting values---
lxw true
false*/
package main
import "fmt"
//最长不含有重复字符的子串
func lenthOfNonRepeatingSubstr(s string) int {
lastOccurred := make(map[byte]int)
start := 0
maxLength := 0
for i, ch := range []byte(s) {
if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
start = lastI + 1
}
if i-start+1 > maxLength {
maxLength = i - start + 1
}
lastOccurred[ch] = i
}
return maxLength
}
func main() {
fmt.Println(lenthOfNonRepeatingSubstr("abcabcbb")) //3
fmt.Println(lenthOfNonRepeatingSubstr("bbbbb")) //1
fmt.Println(lenthOfNonRepeatingSubstr("pwwkew")) //3
}
四.字符串
其他字符串操作
Fileds,split,join
Contains,Index
Tolower,toupper
Trim,trimRight,trimLeft
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/111729.html原文链接:https://javaforall.cn