```
docs(slice): 更新切片文档添加冒泡排序示例和图解 - 添加切片作为函数参数的详细说明,包含值传递和引用传递的对比 - 实现MySort(值传递)和MySortV2(引用传递)两个版本的冒泡排序函数 - 新增draw.io图表文件cut.drawio展示切片切割的可视化解释 - 在README.md中添加图片引用和更详细的切片函数参数说明 - 完善main.go中的切片操作示例,包括切割、删除和排序演示 - 修正切片作业描述,明确要求实现两个版本的字符串转大写函数 ```
This commit is contained in:
parent
277c5dd15c
commit
3ddebb3900
@ -177,7 +177,7 @@ fmt.Println(s2) // [99 2 3]
|
|||||||
```go
|
```go
|
||||||
s := []int{0, 1, 2, 3, 4, 5}
|
s := []int{0, 1, 2, 3, 4, 5}
|
||||||
|
|
||||||
// 创建子切片:s[low:high] (不包括high)
|
// 创建子切片:s[low:high] (不包括high) [start: end)
|
||||||
sub1 := s[1:4] // [1, 2, 3]
|
sub1 := s[1:4] // [1, 2, 3]
|
||||||
fmt.Println(sub1)
|
fmt.Println(sub1)
|
||||||
|
|
||||||
@ -187,30 +187,46 @@ sub3 := s[2:] // [2, 3, 4, 5]
|
|||||||
sub4 := s[:] // [0, 1, 2, 3, 4, 5] 复制整个切片
|
sub4 := s[:] // [0, 1, 2, 3, 4, 5] 复制整个切片
|
||||||
```
|
```
|
||||||
|
|
||||||
## 切片作为函数参数
|

|
||||||
|
|
||||||
|
## 切片作为函数参数 (重点: 90%)
|
||||||
|
|
||||||
**原理**:切片作为参数传递时,是引用传递,函数内修改会影响调用者。高效但需注意副作用。
|
**原理**:切片作为参数传递时,是引用传递,函数内修改会影响调用者。高效但需注意副作用。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func sum(nums []int) int {
|
// 冒泡排序: 值传递
|
||||||
total := 0
|
// 返回一个新的切片(排序的结果)
|
||||||
for _, num := range nums {
|
func MySort(arr []int) []int {
|
||||||
total += num
|
// 冒泡排序
|
||||||
}
|
for i := 0; i < len(arr)-1; i++ {
|
||||||
return total
|
for j := 0; j < len(arr)-1-i; j++ {
|
||||||
|
if arr[j] > arr[j+1] {
|
||||||
|
// 值交换: a, b = b, a
|
||||||
|
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
// 冒泡排序: 引用传递
|
||||||
s := []int{1, 2, 3, 4, 5}
|
// 直接修改传入的切片(在原切片上进行排序)
|
||||||
result := sum(s)
|
func MySortV2(arr *[]int) {
|
||||||
fmt.Println(result) // 15
|
// 冒泡排序
|
||||||
|
for i := 0; i < len(*arr)-1; i++ {
|
||||||
|
for j := 0; j < len(*arr)-1-i; j++ {
|
||||||
|
if (*arr)[j] > (*arr)[j+1] {
|
||||||
|
// 值交换: a, b = b, a
|
||||||
|
(*arr)[j], (*arr)[j+1] = (*arr)[j+1], (*arr)[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 总结
|
## 总结
|
||||||
切片是 Golang 中比较有特色的一种数据类型,既为我们操作集合类型的数据提供了便利的方式,是又能够高效的在函数间进行传递,因此在代码中切片类型被使用的相当广泛
|
切片是 Golang 中比较有特色的一种数据类型,既为我们操作集合类型的数据提供了便利的方式,是又能够高效的在函数间进行传递,因此在代码中切片类型被使用的相当广泛
|
||||||
|
|
||||||
|
|
||||||
## 作业
|
## 作业
|
||||||
|
|
||||||
请同学们完成以下切片练习题,以巩固对Go语言切片的理解:
|
请同学们完成以下切片练习题,以巩固对Go语言切片的理解:
|
||||||
@ -231,6 +247,6 @@ func main() {
|
|||||||
创建一个切片,拷贝到另一个切片,修改原切片,验证拷贝是否独立。
|
创建一个切片,拷贝到另一个切片,修改原切片,验证拷贝是否独立。
|
||||||
|
|
||||||
6. **函数参数**
|
6. **函数参数**
|
||||||
编写一个函数,接受字符串切片作为参数,将所有字符串转换为大写并返回新切片。
|
编写一个函数,接受字符串切片作为参数,将所有字符串转换为大写并返回新切片, 需要实现2个版本, 1.一个值版本, 2. 指针版本。
|
||||||
|
|
||||||
请将代码写在 `main.go` 文件中,并运行测试。
|
请将代码写在 `main.go` 文件中,并运行测试。
|
||||||
|
|||||||
110
day02/slice/cut.drawio
Normal file
110
day02/slice/cut.drawio
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<mxfile host="65bd71144e">
|
||||||
|
<diagram id="c8kls0yAax6bhxLxDwx2" name="第 1 页">
|
||||||
|
<mxGraphModel dx="1134" dy="1642" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||||
|
<root>
|
||||||
|
<mxCell id="0"/>
|
||||||
|
<mxCell id="1" parent="0"/>
|
||||||
|
<mxCell id="14" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="2" target="13">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="60" y="-1030"/>
|
||||||
|
<mxPoint x="60" y="-770"/>
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="15" value="切:&nbsp; s2 := s1[1:3]&nbsp;&nbsp;<div><div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 12px; line-height: 18px; white-space-collapse: preserve;"><span style="color: rgb(136, 132, 111);"> [start: end)</span></div></div>" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="14">
|
||||||
|
<mxGeometry x="-0.0897" y="-2" relative="1" as="geometry">
|
||||||
|
<mxPoint as="offset"/>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="2" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="220" y="-1070" width="370" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="3" value="s1: []int{1,2,3}" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="220" y="-1100" width="110" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4" value="arry ptr" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="250" y="-1045" width="90" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="5" value="len(3)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="360" y="-1045" width="90" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="6" value="cap(3)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="480" y="-1045" width="90" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="7" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="220" y="-930" width="370" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="8" style="edgeStyle=none;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.203;entryY=0.017;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="4" target="7">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="9" value="1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="250" y="-920" width="80" height="40" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="10" value="2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="360" y="-920" width="80" height="40" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="11" value="3" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="464" y="-920" width="80" height="40" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="12" value="arr1: [3]int" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="220" y="-960" width="60" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="21" style="edgeStyle=none;html=1;exitX=0.25;exitY=0;exitDx=0;exitDy=0;entryX=0.25;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="13" target="7">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="13" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="220" y="-810" width="370" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="18" value="len(2)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="369" y="-790" width="90" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="19" value="cap(3)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="480" y="-790" width="90" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="20" value="arry ptr" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="250" y="-790" width="90" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="22" value="无法访问 除切割之外的数据" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="-760" width="150" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="23" value="补充了offset: 1" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="440" y="-840" width="130" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="25" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="130" y="-690" width="30" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="30" style="edgeStyle=none;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="26" target="27">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="26" value="s2[0]&nbsp;" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="220" y="-675" width="60" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="31" style="edgeStyle=none;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="27" target="28">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="27" value="arr1" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="320" y="-675" width="60" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="32" style="edgeStyle=none;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="28" target="29">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="28" value="arr1[0 + 1]" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="440" y="-675" width="60" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="33" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.75;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="29" target="10">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="700" y="-660"/>
|
||||||
|
<mxPoint x="700" y="-980"/>
|
||||||
|
<mxPoint x="420" y="-980"/>
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="29" value="arr1[1]" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="544" y="-675" width="60" height="30" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
</root>
|
||||||
|
</mxGraphModel>
|
||||||
|
</diagram>
|
||||||
|
</mxfile>
|
||||||
BIN
day02/slice/image-5.png
Normal file
BIN
day02/slice/image-5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
BIN
day02/slice/image-6.png
Normal file
BIN
day02/slice/image-6.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 80 KiB |
@ -86,7 +86,6 @@ func main() {
|
|||||||
注意: 数组是引用类型吗? 不是
|
注意: 数组是引用类型吗? 不是
|
||||||
1. 如果是数组: 是2块隔离的内存地址空间(线性分配)
|
1. 如果是数组: 是2块隔离的内存地址空间(线性分配)
|
||||||
2. 如果是切片: 底层数组是同一块内存地址空间
|
2. 如果是切片: 底层数组是同一块内存地址空间
|
||||||
浅拷贝: 赋值 底层数据没有copy一份, 还是指向同一块内存地址空间
|
|
||||||
*/
|
*/
|
||||||
fmt.Println("== 切片是一种引用类型 ==")
|
fmt.Println("== 切片是一种引用类型 ==")
|
||||||
slice7 := []int{1, 2, 3}
|
slice7 := []int{1, 2, 3}
|
||||||
@ -101,7 +100,8 @@ func main() {
|
|||||||
切片的拷贝
|
切片的拷贝
|
||||||
如果希望把 底层arry里面的数据copy到另一个切片中
|
如果希望把 底层arry里面的数据copy到另一个切片中
|
||||||
使用内置函数: copy(目标切片, 源切片)
|
使用内置函数: copy(目标切片, 源切片)
|
||||||
深拷贝: 底层数据已copy一份 申请一片新的内存来存储这个值
|
拷贝: 底层数据已copy一份 申请一片新的内存来存储这个值, 只copy一层, 浅拷贝
|
||||||
|
深copy 是递归copy, 需要自己实现, 依赖三方库, 非常耗性能, 一般不推荐使用, 最常见的方式是使用json序列化和反序列化来实现深copy
|
||||||
*/
|
*/
|
||||||
fmt.Println("== 切片的拷贝 ==")
|
fmt.Println("== 切片的拷贝 ==")
|
||||||
slice9 := make([]int, len(slice7))
|
slice9 := make([]int, len(slice7))
|
||||||
@ -110,4 +110,71 @@ func main() {
|
|||||||
slice9[0] = 2000
|
slice9[0] = 2000
|
||||||
fmt.Println("slice7:", slice7)
|
fmt.Println("slice7:", slice7)
|
||||||
fmt.Println("slice9:", slice9)
|
fmt.Println("slice9:", slice9)
|
||||||
|
|
||||||
|
fmt.Println("== 切片 切割(共享底层数组) ==")
|
||||||
|
slice10 := []int{1, 2, 3}
|
||||||
|
fmt.Println("slice10:", slice10)
|
||||||
|
slice11 := slice10[1:3]
|
||||||
|
fmt.Println("slice11:", slice11)
|
||||||
|
slice11[0] = 200
|
||||||
|
fmt.Println("slice10:", slice10)
|
||||||
|
fmt.Println("slice11:", slice11)
|
||||||
|
|
||||||
|
/*
|
||||||
|
切片的删除
|
||||||
|
切片的删除是通过切片操作实现的,即使用切片语法来移除指定范围的元素。
|
||||||
|
*/
|
||||||
|
fmt.Println("== 切片的删除 ==")
|
||||||
|
slice12 := []int{1, 2, 3, 4, 5}
|
||||||
|
fmt.Println("slice12:", slice12)
|
||||||
|
|
||||||
|
// 1. 从老切片中过滤出需要的元素
|
||||||
|
// 2. 重新组合成一个新的切片
|
||||||
|
// 删除索引为2的元素 (值3)
|
||||||
|
slice12 = append(slice12[:2], slice12[3:]...)
|
||||||
|
fmt.Println("slice12:", slice12)
|
||||||
|
|
||||||
|
/*
|
||||||
|
函数作为参数
|
||||||
|
*/
|
||||||
|
fmt.Println("== 函数作为参数 ==")
|
||||||
|
slice13 := []int{5, 3, 4, 1, 2}
|
||||||
|
fmt.Println("排序前:", slice13)
|
||||||
|
// 值传递
|
||||||
|
slice13_sorted := MySort(slice13)
|
||||||
|
fmt.Println("排序后(值传递):", slice13_sorted)
|
||||||
|
fmt.Println("原切片slice13:", slice13)
|
||||||
|
|
||||||
|
// 引用传递
|
||||||
|
MySortV2(&slice13)
|
||||||
|
fmt.Println("排序后(引用传递):", slice13)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 冒泡排序: 值传递
|
||||||
|
// 返回一个新的切片(排序的结果)
|
||||||
|
func MySort(arr []int) []int {
|
||||||
|
// 冒泡排序
|
||||||
|
for i := 0; i < len(arr)-1; i++ {
|
||||||
|
for j := 0; j < len(arr)-1-i; j++ {
|
||||||
|
if arr[j] > arr[j+1] {
|
||||||
|
// 值交换: a, b = b, a
|
||||||
|
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
// 冒泡排序: 引用传递
|
||||||
|
// 直接修改传入的切片(在原切片上进行排序)
|
||||||
|
func MySortV2(arr *[]int) {
|
||||||
|
// 冒泡排序
|
||||||
|
for i := 0; i < len(*arr)-1; i++ {
|
||||||
|
for j := 0; j < len(*arr)-1-i; j++ {
|
||||||
|
if (*arr)[j] > (*arr)[j+1] {
|
||||||
|
// 值交换: a, b = b, a
|
||||||
|
(*arr)[j], (*arr)[j+1] = (*arr)[j+1], (*arr)[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user