golang 数组基础操作

四种初始化数组的方式

package main
import(
	"fmt"
)

func main(){
	 //四种初始化数组的方式
	 var numArr01 [3]int = [3]int{1,2,3}
	 fmt.Println("numArr01 = ",numArr01)

	 var numArr02 = [3]int{4,5,6}
	 fmt.Println("numArr02 = ",numArr02)

	 var numArr03 = [...]string{0:"张三",1:"李四",2:"王五"}
	 fmt.Println("numArr03 = ",numArr03)

     //如果长度不确定那么就用 ... 来表示长度 
	 numArr04 := [...]string{1:"狂徒",0:"法外",2:"张三"}
	 fmt.Println("numArr04 = ",numArr04)

	 /*
      结果
	  numArr01 =  [1 2 3]
	  numArr02 =  [4 5 6]
	  numArr03 =  [张三 李四 王五]
	  numArr04 =  [法外 狂徒 张三]
	 */
}

for index,value := range(array) 循环基础用户

package main
import(
	"fmt"
)

func main(){
	numArr := [...]string{1:"狂徒",0:"法外",2:"张三"}
	for index,value := range(numArr){
		fmt.Printf("index = %d value = %v\n",index,value)
	}
	/*
	index = 0 value = 法外
	index = 1 value = 狂徒
	index = 2 value = 张三
	*/
}

引用值改变数组里面的值

package main
import(
	"fmt"
)

//使用引用传参
//[3] 这个长度必须是数组的长度 不能多和少也不能...否则会报错
func test01(arr *[3]string){
   //下面也需要指针标识
   (*arr)[0] = "狂徒1"
}

func main(){
	numArr1 := [...]string{1:"狂徒",0:"法外",2:"张三"}
	test01(&numArr1)
	fmt.Println(numArr1)
	//[狂徒1 狂徒 张三]
}

上一篇:BZOJ1218激光炸弹


下一篇:Educational Codeforces Round 109