将整数切片附加到整数切片切片会修改附加的切片

时间:2016-06-18 17:14:58

标签: go append slice

我试图将一片整数附加到由整数切片组成的切片上。当我打印切片时,它会按预期显示。但是,当我将切片附加到切片切片时,内容会发生变化。

package main

import "fmt"

var myGraph [8][8]bool //the graph

var visited [8]bool //an array that marks if visited

var path []int //a slice to store a possible path

var paths [][]int

func dfs(src int, dest int) {
    //add current node to path
    path = append(path, src)

    //mark current node as visited
    visited[src] = true

    //if the current node is the destination
    //print the path and return
    if src == dest {
        fmt.Println(path)
        paths = append(paths, path) //I'm trying to push the path slice into the paths slice
        return
    }

    for i := 1; i <= 7; i++ { //loop through all nodes

        //if ith node is a neighbour of the current node and it is not visited
        if myGraph[src][i] && visited[i] == false {

            // call dfs on the current node
            dfs(i, dest)

            //mark the current node as unvisited
            //so that we can other paths to the final destination
            visited[i] = false

            //re-slice the slice - get rid of the current node
            path = path[:len(path)-1]
        }

    }

}

func main() {
    path = make([]int, 0, 8)
    paths = make([][]int, 0, 10)

    //creating the graph
    myGraph[1] = [...]bool{false, false, true, true, false, false, true, false}
    myGraph[2] = [...]bool{false, true, false, true, false, true, false, false}
    myGraph[3] = [...]bool{false, true, true, false, true, false, true, false}
    myGraph[4] = [...]bool{false, false, false, true, false, false, true, false}
    myGraph[5] = [...]bool{false, false, true, false, false, false, true, false}
    myGraph[6] = [...]bool{false, true, false, true, true, false, false, true}
    myGraph[7] = [...]bool{false, false, false, false, false, false, true, false}

    //call dfs by feeding in the source and the destination
    dfs(1, 7)
    fmt.Println(paths)
}

Output:
[1 2 5 6 7]
[1 3 2 5 6 7]
[1 3 4 6 7]
[1 3 6 7]
[1 6 7]
[[1 6 7 3 2 5] [1 6 7 3 2] [1 6 7 3 2] [1 6 7 3 2 5] [1 6 7 3 2] [1 6 7 3] [1 6 7]]

如您所见,切片切片最后由主函数打印。但是,其内容与dfs()打印的各个切​​片不同。

1 个答案:

答案 0 :(得分:1)

让我们更改一下这段代码:

int A[][]=new int[4][4];
char B[][]=new char[4][4];
A=|1234
  |5678
  |9 10 11 12
  |13 14 15 16 
B=|ABCD
  |EFGH
  |IJKL
  |MNOP

result

A=|ABCD
  |EFGH
  |IJKL
  |MNOP

B=|1234
  |5678
  |9 10 11 12
  |13 14 15 16

现在它按预期工作了!为什么?因为切片是引用类型,而不是值类型;表示当您稍后更改var myGraph [8][8]bool //the graph var visited [8]bool //an array that marks if visited var path []int //a slice to store a possible path var paths [][]int func dfs(src int, dest int) { //add current node to path path = append(path, src) //mark current node as visited visited[src] = true //if the current node is the destination //print the path and return if src == dest { // (A) buffer := make([]int, len(path)) copy(buffer, path) fmt.Println(buffer) paths = append(paths, buffer) //I'm trying to push the path slice into the paths slice // fmt.Println(path) // paths = append(paths, path) //I'm trying to push the path slice into the paths slice return } for i := 1; i <= 7; i++ { //loop through all nodes //if ith node is a neighbour of the current node and it is not visited if myGraph[src][i] && visited[i] == false { // call dfs on the current node dfs(i, dest) //mark the current node as unvisited //so that we can other paths to the final destination visited[i] = false //re-slice the slice - get rid of the current node path = path[:len(path)-1] } } } func main1() { // path = make([]int, 0, 8) // paths = make([][]int, 0, 10) //creating the graph myGraph[1] = [...]bool{false, false, true, true, false, false, true, false} myGraph[2] = [...]bool{false, true, false, true, false, true, false, false} myGraph[3] = [...]bool{false, true, true, false, true, false, true, false} myGraph[4] = [...]bool{false, false, false, true, false, false, true, false} myGraph[5] = [...]bool{false, false, true, false, false, false, true, false} myGraph[6] = [...]bool{false, true, false, true, true, false, false, true} myGraph[7] = [...]bool{false, false, false, false, false, false, true, false} //call dfs by feeding in the source and the destination dfs(1, 7) fmt.Println(paths) } 时,除非您像path处所做的那样拍摄快照,否则您将看到新值。

相关问题