dongwu9972 2016-07-27 07:58
浏览 97
已采纳

Golang:使用切片策略的多维数组导致奇怪的输出

I am using following simple code for 2d array in golang, where APPEND function is resulting in Duplicate values rather than appending.

package main

import "fmt"

func main() {
    var n int
    fmt.Scanf("%d", &n)
    array := [][]int{}
    row := make([]int, n)
    for _, _ = range row {
        for j, _ := range row {
            fmt.Scanf("%d", &row[j])
        }
        fmt.Println("Printing current Row", row)
        array = append(array, row)
        fmt.Println("Printing curent Array", array)
    }
    fmt.Println("Final Array", array)
}

But Strangely this are not going unexpectedly. If suppose i want this thing to happen(input)

2
1 2
3 4

and i run this program i get this in return

2     //Dimension for matrix
1     //Iteration one begins
2
Printing current Row [1 2]
Printing curent Array [[1 2]]
3     //Iteration two begins
4
Printing current Row [3 4]
Printing curent Array [[3 4] [3 4]]
Final Array [[3 4] [3 4]]

I am not getting reason that why APPEND function is resulting in duplicating entries . There by want to know how to correct this also underlying CONCEPT

  • 写回答

2条回答 默认 最新

  • dqfaom241360 2016-07-27 08:20
    关注

    use new slice for each iteration of the first loop, and check for the errors:

    you read new data to the old same slice, this is why you have duplicated data.

    if you want to use append first create array with zero length and cap n:
    array := make([][]int, 0, n)
    then change your first loop to: for i := 0; i < n; i++ {
    and inside this loop make new slice: row := make([]int, n)

    2 way:
    using append (without fmt.Scan error checking):

    package main
    
    import "fmt"
    
    func main() {
        n := 0
        fmt.Scan(&n)
        slice := make([][]int, 0, n)
        for i := 0; i < n; i++ {
            row := make([]int, n)
            for j, _ := range row {
                fmt.Scan(&row[j])
            }
            slice = append(slice, row)
        }
        fmt.Println(slice)
    }
    

    without using append read to s[i][j] (with error checking):

    package main
    
    import "fmt"
    
    func main() {
        var n int
        if m, err := fmt.Scan(&n); m != 1 {
            panic(err)
        }
        s := make([][]int, n)
        for i := 0; i < n; i++ {
            s[i] = make([]int, n)
            for j := 0; j < n; j++ {
                if m, err := fmt.Scan(&s[i][j]); m != 1 {
                    panic(err)
                }
            }
        }
        fmt.Println(s)
    }
    

    input:

    2
    1 2
    3 4
    

    output:

    [[1 2] [3 4]]
    

    I think this test sample code is clear enough to
    show how slice of slice works (with commented output):

    package main
    
    import "fmt"
    
    func main() {
        s1 := [][]int{}
        s2 := []int{1, 2}
        s1 = append(s1, s2)
        fmt.Println(s1) // [[1 2]]
    
        s2[0], s2[1] = 3, 4
        fmt.Println(s1) // [[3 4]]
    
        s1 = append(s1, s2)
    
        fmt.Println(s1) // [[3 4] [3 4]]
    
        s2[0], s2[1] = 30, 40
        fmt.Println(s1) // [[30 40] [30 40]]
    
        fmt.Println(len(s2), cap(s2)) // 2 2
    
        s3 := [][]int{
            []int{1, 2},
            []int{3, 4},
            s2,
            s2,
        }
        fmt.Println(s3) // [[1 2] [3 4] [30 40] [30 40]]
        s2[0] = 100
        fmt.Println(s3) // [[1 2] [3 4] [100 40] [100 40]]
    }
    

    you created slice of slice s1 like this :
    array [0] = row[0], row[1]
    array [1] = row[0], row[1]

    so when you change row it will seen twice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题