【问题标题】:Dynamically append each values into 2D slice将每个值动态附加到 2D 切片中
【发布时间】:2016-02-22 19:47:14
【问题描述】:

我希望有一个数据结构(数组或切片)看起来像这样:

[[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]] 

使得 a 是节点从 "A" 到 "A" 的距离。 (应为 0) b 是节点从 "A" 到 "B" 的距离。 c 是节点从“A”到“C”的距离。

f 是节点从 "B" 到 "A" 的距离。 g 是节点从 "B" 到 "B" 的距离。 (应为 0) h 是节点从“B”到“C”的距离。

现在我创建了一个像这样的切片: var shortestPathSLice = make([][]int, 5) 存储此二维数据。

在函数内的 for 循环中,我尝试如下动态填充此切片:

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

其中 minimumimate[0] 是两个节点之间的最小距离值。

但是,我得到一个错误:第一个参数 append 必须是 slice;有int

谁能告诉我如何在切片的每个元素中动态附加值?

**代码**

    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]
}

【问题讨论】:

  • shortestPathSLice 是一个[][]int 类型的二维数组。所以shortestPathSLice[0][index] 选择0.index 处的int。但是,追加的第一个 arg 必须是切片,因此您的 0index 应省略。
  • 我认为您要做的是:shortestPathSLice[0][index] = lowEstimate[0]。那将分配int? lowEstimate[0] 切片索引 0.index.
  • @RickyA 感谢您的回复。对于第一次迭代,我的 LowEstimate 数组是:[6 12 12 18 27],我希望 shortestPathSLice 作为 [[0 6 0 0 0]] 对于第二次迭代,我的 LowEstimate 数组是:[35 37 38 39],我希望 shortestPathSLice 为 [[0 6 35 0 0]]
  • 好的,这意味着你应该在你的内部循环中使用shortestPathSLice[0][index] = lowEstimate[0]
  • 看看this 初始化二维切片的页面

标签: arrays go append slice


【解决方案1】:

The Go Programming Language Specification

Appending to and copying slices

普通切片中的内置函数追加和复制辅助 操作。对于这两个函数,结果与是否 参数引用的内存重叠。

可变参数函数 append 将零个或多个值 x 附加到 s 的 类型 S,必须是切片类型,并返回结果切片, 也是 S 类型的。值 x 被传递给 ...T 类型的参数 其中 T 是 S 的元素类型和相应的参数传递 规则适用。作为一种特殊情况,append 也接受第一个参数 可分配给类型 []byte 并带有字符串类型的第二个参数 后跟...。这种形式附加了字符串的字节。

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

如果 s 的容量不足以容纳附加值, append 分配一个新的,足够大的底层数组,适合 现有的切片元素和附加值。否则, append 重用底层数组。

例如,使用append 并使用索引,

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

输出:

[[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]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2016-10-20
    • 2019-10-17
    • 2019-04-12
    • 2019-12-30
    • 1970-01-01
    相关资源
    最近更新 更多