go语音基础之二维数组
Posted nulige
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(" ") } 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]]
以上是关于go语音基础之二维数组的主要内容,如果未能解决你的问题,请参考以下文章