duanqun7761 2016-07-11 01:10
浏览 37
已采纳

无法使用Golang交换2D阵列切片的元素

I am trying to write a simple function that transposes a square matrix (i.e. swaps the columns and rows). Here is the function in question:

func Transpose(a [][]bool) {
    for i := 0; i < len(a); i++ {
        for j := 0; j < len(a[i]); j++ {
            a[i][j], a[j][i] = a[j][i], a[i][j]
        }
    }
}

It doesn't seem to work. If I run this function

func TestTranspose(t *testing.T) {
    a := make([][]bool, 3)
    a[0] = []bool{true, true, true}
    a[1] = []bool{false, true, false}
    a[2] = []bool{true, true, true}

    fmt.Println(BoolArrayViz(a))

    Transpose(a)

    fmt.Println(BoolArrayViz(a))
}

It gives the following output (BoolArrayViz simply prints the bool as '*' if true ' ' if false):

***
 * 
***

***
 * 
***

I believe it has something to do with pointers and the fact that I'm passing around slices. Any help is appreciated!

  • 写回答

2条回答 默认 最新

  • dtrt2368 2016-07-11 02:50
    关注

    Your Transpose logic is not correct (you transposed it twice),
    See this working test sample (not using swap to demonstrate the idea):

    package main
    
    import "fmt"
    
    func Transpose(a [][]bool) {
        n := len(a)
        b := make([][]bool, n)
        for i := 0; i < n; i++ {
            b[i] = make([]bool, n)
            for j := 0; j < n; j++ {
                b[i][j] = a[j][i]
            }
        }
        copy(a, b)
    }
    func main() {
        a := [][]bool{
            []bool{true, true, true},
            []bool{false, true, false},
            []bool{true, true, true},
        }
        BoolArrayViz(a)
        Transpose(a)
        BoolArrayViz(a)
    }
    
    func BoolArrayViz(a [][]bool) {
        n := len(a)
        for i := 0; i < n; i++ {
            for j := 0; j < n; j++ {
                if a[i][j] {
                    fmt.Printf("*")
                } else {
                    fmt.Printf(" ")
                }
            }
            fmt.Println()
        }
        fmt.Println()
    }
    

    output:

    ***
     * 
    ***
    
    * *
    ***
    * *
    

    using swap (the inner loop, j should start at i+1 instead of 0):

    package main
    
    import "fmt"
    
    func Transpose(a [][]bool) {
        n := len(a)
        for i := 0; i < n; i++ {
            for j := i + 1; j < n; j++ {
                a[i][j], a[j][i] = a[j][i], a[i][j]
            }
        }
    }
    func main() {
        a := [][]bool{
            []bool{true, true, true},
            []bool{false, true, false},
            []bool{true, true, true},
        }
        BoolArrayViz(a)
        Transpose(a)
        BoolArrayViz(a)
    }
    
    func BoolArrayViz(a [][]bool) {
        n := len(a)
        for i := 0; i < n; i++ {
            for j := 0; j < n; j++ {
                if a[i][j] {
                    fmt.Printf("*")
                } else {
                    fmt.Printf(" ")
                }
            }
            fmt.Println()
        }
        fmt.Println()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题