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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分