go语言基础之二维数组

1、二维数组

示例:

package main //必须有个main包

import "fmt"

func main() {
//有多少个[]就是多少维
//有多少个[]就用多少个循环
var a [3][4]int k := 0
for i := 0; i < 3; i++ {
for j := 0; j < 4; j++ {
k++
a[i][j] = k
fmt.Printf("a[%d][%d] = %d, ", i, j, a[i][j])
}
fmt.Printf("\n")
} fmt.Println("a = ", a) //有3个元素,每个元素又是一维数组[4]int
b := [3][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
fmt.Println("b = ", b) //部分初始化,没有初始化的值为0
c := [3][4]int{{1, 2, 3}, {5, 6, 7, 8}, {9, 10}}
fmt.Println("c = ", c) d := [3][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
fmt.Println("d = ", d) e := [3][4]int{1: {5, 6, 7, 8}}
fmt.Println("e = ", e)
}

执行结果:

a[0][0] = 1, a[0][1] = 2, a[0][2] = 3, a[0][3] = 4,
a[1][0] = 5, a[1][1] = 6, a[1][2] = 7, a[1][3] = 8,
a[2][0] = 9, a[2][1] = 10, a[2][2] = 11, a[2][3] = 12,
a = [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
b = [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
c = [[1 2 3 0] [5 6 7 8] [9 10 0 0]]
d = [[1 2 3 4] [5 6 7 8] [0 0 0 0]]
e = [[0 0 0 0] [5 6 7 8] [0 0 0 0]]

  

上一篇:多远线性算法预测房价


下一篇:隐藏el-table-column过多的内容并进行浮窗展示