dtwncxs3547 2015-11-20 10:46
浏览 53

将每个值动态附加到GOLANG中的2D切片中

I wish to have a datastructure(array or slice) to look like this :

[[a b c d e][f g h i j] [k l m n o] [p q r s t] [u v w x y]] 

such that a is the distance between node from "A" to "A" . (which shall be 0) b is the distance between node from "A" to "B" . c is the distance between node from "A" to "C" .

f is the distance between node from "B" to "A" . g is the distance between node from "B" to "B" . (which shall be 0) h is the distance between node from "B" to "C" .

Now I have created a slice like : var shortestPathSLice = make([][]int, 5) to store this 2D data.

In my for loop within a function, I am trying to fill this slice dynamically as follows :

shortestPathSLice = append(shortestPathSLice[0][index], lowEstimate[0])

where lowestimate[0] is value of the smallest distances between two nodes.

However, I get an error with this : first argument to append must be slice; have int

Can anyone tell me how can I dynamically append values in EACH ELEMENT in my slice ?

**CODE **

    var shortestPathSLice = make([][]int, 5)
    for index := 0; index < len(t.Location_ids); index++ {
    lowEstimate := make([]int, len(priceestimatestruct.Prices))
        for i := 0; i < len(priceestimatestruct.Prices); i++ {
            lowEstimate[i] = priceestimatestruct.Prices[i].LowEstimate
        }

        sort.Ints(lowEstimate)
        fmt.Println("LowEstimate array : ", lowEstimate)
        shortestPathSLice[0] = make([]int, len(lowEstimate))
        shortestPathSLice[0][index] = lowEstimate[0]
}
  • 写回答

1条回答 默认 最新

  • dsq1982 2015-11-20 12:30
    关注

    The Go Programming Language Specification

    Appending to and copying slices

    The built-in functions append and copy assist in common slice operations. For both functions, the result is independent of whether the memory referenced by the arguments overlaps.

    The variadic function append appends zero or more values x to s of type S, which must be a slice type, and returns the resulting slice, also of type S. The values x are passed to a parameter of type ...T where T is the element type of S and the respective parameter passing rules apply. As a special case, append also accepts a first argument assignable to type []byte with a second argument of string type followed by .... This form appends the bytes of the string.

    append(s S, x ...T) S  // T is the element type of S
    

    If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large underlying array that fits both the existing slice elements and the additional values. Otherwise, append re-uses the underlying array.

    For example, using append and using an index,

    package main
    
    import "fmt"
    
    func main() {
        { // using append
            dim := 5
            matrix := make([][]int, dim) // dim*dim matrix
            for i := 0; i < dim; i++ {
                matrix[i] = make([]int, 0, dim)
                vector := make([]int, dim)
                for j := 0; j < dim; j++ {
                    vector[j] = i*dim + j
                    matrix[i] = append(matrix[i], vector[j])
                }
            }
            fmt.Println(matrix)
        }
        { // using index
            dim := 5
            matrix := make([][]int, dim) // dim*dim matrix
            for i := range matrix {
                matrix[i] = make([]int, dim)
                vector := make([]int, dim)
                for j := range matrix[i] {
                    vector[j] = i*dim + j
                    matrix[i][j] = vector[j]
                }
            }
            fmt.Println(matrix)
        }
    }
    

    Output:

    [[0 1 2 3 4] [5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]]
    [[0 1 2 3 4] [5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答