假设我想创建一个看起来像
的二维数组/* [
[2],
[3,4],
[6,5,7],
[4,1,8,3]
] */
如何使用go创建它?
通常情况下,如果我有一个像大小相同的二维数组,如
/* [4,1,8,1],
[2,1,3,3],
[7,1,4,3]
*/
matrix := make([][]int, 4)
for i := range matrix {
matrix[i] = make([]int, 3)
}
答案 0 :(得分:1)
例如,
package main
import (
"fmt"
)
func main() {
matrix := make([][]int, 4)
for i := range matrix {
matrix[i] = make([]int, i+1)
}
fmt.Println(matrix)
}
输出:
[[0] [0 0] [0 0 0] [0 0 0 0]]
或者
package main
import (
"fmt"
)
func main() {
matrix := [][]int{{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}}
fmt.Println(matrix)
}
输出:
[[2] [3 4] [6 5 7] [4 1 8 3]]
答案 1 :(得分:1)
您可以在matrix := make([][]int, 4)
for i := range matrix {
matrix[i] = make([]int, i+1)
}
创建不同尺寸的切片:
npm install --save angular-base64