码迷,mamicode.com
首页 > 编程语言 > 详细

go语音基础之二维数组

时间:2019-01-08 15:31:10      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:一维数组   pack   数组   package   初始化   col   执行   printf   语音   

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

  

 

go语音基础之二维数组

标签:一维数组   pack   数组   package   初始化   col   执行   printf   语音   

原文地址:https://www.cnblogs.com/nulige/p/10238078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!