配列とは何ですか?
配列
アレイは、固定長からなる特定のタイプの要素の配列であり、それはゼロ以上の要素のアレイから構成されてもよいです
アレイ法の定義?
道
package main
import "fmt"
func arraytest() {
var x [3] int
fmt.Println(x)
}
// 输出 [0 0 0]
func main() {
arraytest()
}
使用クイックは、配列を宣言します
x3 :=[3] int {112,78}
fmt.Println(x3)
输出 [112 78 0]
//値の配列を取得します
var x2[3] int
x2[0] = 1 //第一个元素
x2[1] = 11
x2[2] = 22
fmt.Println(x2)
输出 [1 11 22]
//データの配列を取得するためにforループを使用します
x6 :=[...] int {3,5,6,82,34}
for i :=0; i< len(x6);i++{
fmt.Println(i,x6[i])
}
// 输出 如下 0 表示索引号, 3表示 对应的具体值
0 3
1 5
2 6
3 82
4 34
言語//の使用範囲を提供することは邪魔に行きます
for i,v := range x6{
fmt.Printf("%d of x6 is %d\n",i,v)
}
// 输出
0 3
1 5
2 6
3 82
4 34
//アクセスのみの要素
for _,v1 :=range x6 {
fmt.Printf("x6 array value is %d\n",v1)
}
// 输出
0 of x6 is 3
1 of x6 is 5
2 of x6 is 6
3 of x6 is 82
4 of x6 is 34
スライス
スライス(スライス)とは何ですか?
スライス(スライス)が容易に使用する特殊なデータ構造、このデータ構造の一種Golangであり、データセットを管理
//スライスを定義します
メインパッケージ
のインポート「FMT」を
FUNC slicetest(){
A1:= [4] INT {1,3,7,22}
fmt.Println(A1)
// 输出
[1 3 7 22]
var b1 []int = a1[1:4]
fmt.Println(b1,len(b1))
// 输出
[3 7 22] 3
}
FUNCのmain(){
slicetest()
}
//セクションでは、使用して文を作る
能力を示していない場合、彼は同じ長さとなり、時間の初期化関数のスライスを作成、初期化時にその容量を示している場合
s1 :=make([]int, 5)
fmt.Printf("The length of s1: %d\n",len(s1)) # 长度
fmt.Printf("The capacity of; s1: %d\n",cap(s1)) # cap 容量
fmt.Printf("The value of s1:%d\n",s1)
// 输出
The length of s1: 5
The capacity of; s1: 5
The value of s1:[0 0 0 0 0]
s12 :=make([]int, 5,8) # 指明长度为 5, 容量为8
fmt.Printf("The length of s12: %d\n",len(s12))
fmt.Printf("The capacity of; s12: %d\n",cap(s12))
fmt.Printf("The value of s12:%d\n",s12)
// 输出
The length of s12: 5
The capacity of; s12: 8
The value of s12:[0 0 0 0 0]
//
s3 := []int{1, 2, 3, 4, 5, 6, 7, 8}
s4 := s3[3:6]
fmt.Printf("The length of s4: %d\n", len(s4))
fmt.Printf("The capacity of s4: %d\n", cap(s4))
fmt.Printf("The value of s4: %d\n", s4)
// 输出
The length of s4: 3 # 长度为3
The capacity of s4: 5 # 容量为5
The value of s4: [4 5 6]
// 切片与数组的关系
切片的容量可以看成是底层数组元素的个数
s4 是通过s3 切片获得来的,所以s3 的底层数组就是s4 的底层数组,切片是无法向左拓展的,所以s4 无法看到s3 最左边的三个元素,所以s4 的容量为5
// GETスライス
before_slice :=[] int {1,4,5,23,13,313,63,23}
after_slice := before_slice[3:7]
fmt.Println("array before change",before_slice)
// 使用for range 遍历
for i := range after_slice{
after_slice[i]++
}
fmt.Println("after cahnge",before_slice)
//出力
変更前の配列[1 4 5 23 13 313 63 23]
cahnge後、[1 4 5 24 14 314 64 23]
アレイとスライスとの間の差
かどうかは容量スケーラブルな?アレイの容量は、スライス缶を伸ばすことができません
これは、比較することができますか?//同じ長さの配列を比較することができます