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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi