array: var variable name [Len]type
Usage scenarios:
Fixed requirement, use array, cannot exceed specified length
Length is also part of the array type
The underlying memory space is contiguous
Used to store constants
Arrays are a special kind of dictionary .....
Array write, assignment method, array [index]= value
An array is a value type, and if you copy it into another array, the array is modified, and the original array does not change
Array initialization:
???? Defined:???? var variable name [Len]type
?
Example:
Var?a[5]intvar?a[5]stringvar?a[5]float32var?a[5]bool
To access an element in an array:
? Using subscript access:? A[0] accesses the first element. An array of length 5 is accessed as follows: A[0], a[1], a[2], a[3], a[4]
The length in the array:
???? var a[10]int
???? Lengh: =len (a)
The sample code splits the section:
-
Array loop:
-
format%p, used to print memory address
-
& symbol, take variable address
-
Try using variables to loop
-
var?a?[ 10]intfor?i?:=0;i<len (a); i++{??? Fmt. Printf ("%p\n", &a[i])}
-
length:=len (a)? Array length
-
Array initialization:
-
-
Creates another array, modifies the array, and does not modify the original array
-
func?test2 ()?? {??? Var?a[5]int?? =? [5]int? {1,2,3,4,5}??? Var?b[5]?int??? B?=?a??? Fmt. Printf ("%v\n", b)??? B[0]?=?200??? Fmt. Printf ("%v\n", b)??? Fmt. Printf ("%v\n", a)}
-
Array definition:
-
when declaring an array, you need to define the length and type!!!
-
-
array Assignment: Specify subscript to specify element assignment
-
% #以go的方式打印
-
var?d?[ 5]string?=? [5]string{1: "ABC", 4: "Zcq"}//%#?? Print to go!!! Fmt. Printf ("%#%v\n", D) fmt. Printf ("%v\n", a,b,c)
-
Two-dimensional array
-
func?test4 ()?? {??? Two-dimensional array??? Var?a[4][2]int??? for?i:=0;i<4;i++{?????? for?j:=0;i<2;j++{????????? A[i][i]?=? (i+1)? * (j+1)??????}???}??? for?i:=0;i<4;i++{?????? for?j:=0;i<2;j++{????????? Fmt. Printf ("%d", a[i][j])??????}?????? Fmt. Println ()???}}
-
Array Assignment: Random numbers into the array
-
Remember: Go is a strongly typed language, so if the type is different, it is not compiled
-
using the random number module, use the time module
-
r?: =?rand. New (Rand. Newsource (time. Now (). Unix ())
-
func?test5 ()?? {??? Random numbers into the array??? R?:=?rand. New (Rand. Newsource (time. Now (). Unix ()))??? Var?five? [100]int??? Array? To assign a value by index??? For?i:=0;i<len (five); i++{?????? Assignment Operation?????? Five[i]=r.int ()?????? Value Operation?????? Fmt. Printf ("%d\n", Five[i])???}??? Fmt. Printf ("%d\n", Five)}
Array assignment: Random string into array
Go inside is utf-8 code
Sprintf format and return the formatted string, can be directly assigned value!!!
Func?test6 ()?? {?????? Var?a? [100]string??? Var?b?string?=? " Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz "??? For?i:=0;i<len (a); i++{?????? Var?str?string?????? For?j:=0;j<len (b); j++{????????? Index?:=rand. INTN (len (b))????????? Sprintf? format and return the formatted string????????? Str?=?fmt. Sprintf ("%s%c", Str,b[index])??????}?????? A[i]?=?str?????? Fmt. Printf ("a[%d]=%s\n", I,a[i])???}}
Go (day3[array])