1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| 1.自动计算数组的长度,不用再刻意定义。 arr := [...]int{1,2,3,4,5} fmt.Println(arr)
2.数组赋值 a := [3]int{} a[0]=0 a[1]=0 a[2]=0
3.数组和多维数组核心知识点测试代码 func arrayExample1(x [5]int){ fmt.Println(x)
for i:=0;i<len(x);i++{ fmt.Printf("for: x[%d]=%d\n",i,x[i]) }
for k,v := range x{ fmt.Printf("range: x[%d]=%d\n",k,v) } }
func arrayExample2(array2 []int){
for i:=0;i<len(array2);i++{ array2[i] = i+99 } fmt.Println(array2)
array3 := [...]int{1,1,1,1,1} fmt.Println(array3) }
func arrarExample3(){ var arr3 [3][3]int arr3 = [3][3]int{{1,2,4},{2,4,6},{23,23,23}} fmt.Println(arr3)
var arr4 = [...][3]string{ [3]string{"jwgod","23","engineer1"}, [3]string{"jwgod1","24","engineer2"}, [3]string{"jwgod2","25","engineer3"}, [3]string{"jwgod3","26","engineer4"}, [3]string{"jwgod4","27","engineer5"}, [3]string{"jwgod5","28","engineer6"}, [3]string{"jwgod6","29","engineer7"}, }
for _,v := range arr4{ fmt.Println(v) }
for x,y :=range arr4{ for x1,y2 := range y{ fmt.Printf("arr4[%d][%d]=%v\n",x,x1,y2) }
} }
|