针对第一个问题,做了下实验,发现如果改变数组中的元素时是不会改变源数组的,当改变数组中的切片中的元素时是会改变源数组的,我猜是因为切片改变元素时底层数组会改变,所以源数组也跟着改变
package main
import "fmt"
func main() {
complexArray1 := [3][]string{
[]string{"d", "e", "f"},
[]string{"g", "h", "i"},
[]string{"j", "k", "l"},
}
fmt.Printf("The array: %v\n", complexArray1)
array2 := modifyArray(complexArray1)
fmt.Printf("The modified array: %v\n", array2)
fmt.Printf("The original array: %v\n", complexArray1)
array3 := modifyArray1(complexArray1)
fmt.Printf("The modified array: %v\n", array3)
fmt.Printf("The original array: %v\n", complexArray1)
}
func modifyArray(a [3][]string) [3][]string {
a[1] = []string{"d", "e", "p"}
return a
}
func modifyArray1(a [3][]string) [3][]string {
a[1][1] = "v"
return a
}
展开