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 72
| package main
import "fmt"
var arr [3]int var arr2 = [4]int{1,2,3,4}
func main(){ arr3 := [4]float64{1,2,3,4} fmt.Println(arr3)
arr4 :=[...] int{2,3,4}
fmt.Println(arr4)
fmt.Printf("類(lèi)型arr3: %T,類(lèi)型arr4: %T\n",arr3,arr4)
fmt.Println("arr3的長(zhǎng)度為:",len(arr3)) fmt.Println("arr3的下標(biāo)為2:",arr3[2])
for i:=0;i<len(arr3);i++{ fmt.Println(arr3[i]) }
for _,value:=range arr3{
fmt.Println(value) }
array() twoarray() }
func array(){
arr :=[10]int{1,3,2,4,9,2,6,5,4,8} fmt.Println("一開(kāi)始:",arr)
for i:=0;i<len(arr)-1;i++{
for j:=0;j<len(arr)-1-i;j++{
if(arr[j] >arr[j+1]){ temp:=arr[j] arr[j] = arr[j+1] arr[j+1] = temp } fmt.Println(arr) } } fmt.Println("結(jié)束:",arr) }
func twoarray(){
a :=[3][3]int{{1,2,3},{4,5,6},{7,8,9}} for i:=0;i<len(a);i++{
for j:=0;j<len(a[0]);j++{ fmt.Printf("a[%d][%d]=%d\n",i,j,a[i][j]) } }
}
|