package main
import (
"fmt"
)
func main() {
//演示二維數(shù)組的遍歷
var arr3 = [2][3]int{{1,2,3},{4,5,6}}
//for循環(huán)來遍歷
for i :=0;i len(arr3);i++{
for j:=0;j len(arr3[i]);j++{
fmt.Printf("%v\t",arr3[i][j])
}
fmt.Println()
}
//for-range來遍歷二維數(shù)組
for i,v :=range arr3{
for j,v2 :=range v{
fmt.Printf("arr3[%v][%v]=%v \t",i,j,v2)
}
fmt.Println()
}
}
package main //必須有個(gè)main包
import "fmt"
func main() {
//有多少個(gè)[]就是多少維
//有多少個(gè)[]就用多少個(gè)循環(huán)
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個(gè)元素,每個(gè)元素又是一維數(shù)組[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]]
以上就是Golang二維數(shù)組的使用方式的詳細(xì)內(nèi)容,更多關(guān)于Golang二維數(shù)組的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!