加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang中slice处理遇到的一个关于引用的坑

发布时间:2020-12-16 09:21:39 所属栏目:大数据 来源:网络整理
导读:前两天在解扫地机器人算法的问题时,遇到一个坑 部分代码如下: func move2(startPoint Point) [][]Point { allFootPrint := [][]Point{{startPoint}} for i := 0; i N; i++ { allNewFootPrint := make([][]Point,0) for len(allFootPrint) 0 { curFootPrint
前两天在解扫地机器人算法的问题时,遇到一个坑

部分代码如下:

func move2(startPoint Point) [][]Point {
    allFootPrint := [][]Point{{startPoint}}
    for i := 0; i < N; i++ {
        allNewFootPrint := make([][]Point,0)
        for len(allFootPrint) > 0 {
            curFootPrint := allFootPrint[len(allFootPrint)-1]
            allFootPrint = allFootPrint[:len(allFootPrint)-1]
            last := curFootPrint[len(curFootPrint)-1]
            for _,d := range directions {
                nextPoint := Point{last.X + d[0],last.Y + d[1]}
                if !inArray(nextPoint,curFootPrint) {
                    // 必须复制一份数据出来,否则会发生路径重复
                    newCurFootPrint := make([]Point,len(curFootPrint))
                    copy(newCurFootPrint,curFootPrint)

                    allNewFootPrint = append(allNewFootPrint,append(newCurFootPrint,nextPoint))
                }
            }
        }
        allFootPrint = allNewFootPrint
    }
    return allFootPrint
}

这处注释的地方非常关键,如果不复制出来,会导至allNewFootPrint中出现连续的两个相同路径,并且不是所有的路径都出问题,只会在一轮循环结束后,新一轮循环开始时才会出现,当时查了半天才查出问题。

现在把这个问题单独拎出来,分享给大家。

package main

import "fmt"

func main() {
    a := []int{1,2,3,4,5,6}
    x := a[:2]
    x = append(x,9)
    fmt.Println(x)
    fmt.Println(a)
}

输出:

[1 2 9]
[1 2 9 4 5 6]

上面的操作很简单,就是从a切片里取出前2个,然后再追加一个数字9进去。
结果我们发现x是正确的,但a切片也随之发生了改动。
这说明x其实只是a切片的一个引用,对x的任何改动,都会影响到a。
这简直是挖了个天大的坑,机器人的问题也正是这里的问题。
只能copy出一个新的slice方能解决这个问题

package main

import "fmt"

func main() {
    a := []int{1,6}

    c := make([]int,2)
    copy(c,a[:2])

    c = append(c,9)
    fmt.Println(c)
    fmt.Println(a)
}

输出:

[1 2 9]
[1 2 3 4 5 6]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读