diff --git a/eBook/07.1.md b/eBook/07.1.md index 3d2d202..440ac33 100644 --- a/eBook/07.1.md +++ b/eBook/07.1.md @@ -4,7 +4,7 @@ 数组是具有相同`唯一类型`的一组已编号且长度固定的数据项序列(这是一种同构的数据结构);这种类型可以是任意的原始类型例如整形、字符串或者自定义类型。数组长度必须是一个常量表达式,并且必须是一个非负整数。数组长度也是数组类型的一部分,所以[5]int和[10]int是属于不同类型的。数组的编译时值初始化是按照数组顺序完成的(如下)。 **注意事项** -如果我们想让数组元素类型为任意类型的话可以使用空接口作为类型(参考[第11章](11.9.md)。当使用值时我们必须先做一个类型判断(参考[第11章](11.3.md)。 +如果我们想让数组元素类型为任意类型的话可以使用空接口作为类型(参考[第11章](11.9.md))。当使用值时我们必须先做一个类型判断(参考[第11章](11.3.md))。 数组元素可以通过`索引`(位置)来读取(或者修改),索引从0开始,第一个元素索引为0,第二个索引为1,以此类推。(数组以0开始在所有类C语言中是相似的)。元素的数目,也称为`length`或者数组大小必须是固定的并且在声明该数组时就给出(编译时需要知道数组长度以便分配内存);数组长度最大为2Gb。 @@ -22,9 +22,9 @@ arr1的长度是5,索引范围从0到len(arr1)-1 对索引项为i的数组元素赋值可以这么操作:arr[i] = value,所以数组是`可变的`。 -只有有效的索引可以被使用,当使用等于或者大于len(arr1)的索引时:如果编译器可以检测到,会给出索引超限的提示信息;如果检测不到的话编译会通过而运行时会panic:(参考[第13章](13.0.md) +只有有效的索引可以被使用,当使用等于或者大于len(arr1)的索引时:如果编译器可以检测到,会给出索引超限的提示信息;如果检测不到的话编译会通过而运行时会panic:(参考[第13章](13.0.md)) -runtime error: index out of range + runtime error: index out of range 由于索引的存在,遍历数组的方法自然就是使用for-construct: @@ -73,7 +73,7 @@ IDIOM: ... } -在这里i也是数组的索引。当然这两种for-construct方式对于分片(slices)(参考[第7章](07.2.md)来说也同样适用。 +在这里i也是数组的索引。当然这两种for-construct方式对于分片(slices)(参考[第7章](07.2.md))来说也同样适用。 **问题 7.1** 下面代码段的输出是什么? @@ -98,6 +98,7 @@ Go语言中的数组是一种`值类型`(不像C/C++中是指向首元素的 如果你想修改原数组,那么arr1必须通过&操作符以引用方式传过来,例如func1(&arr1),下面是一个例子 Example 7.2 [pointer_array.go](examples/chapter_7/pointer_array.go): + package main import "fmt" func f(a [3]int) { fmt.Println(a) } @@ -108,7 +109,7 @@ Example 7.2 [pointer_array.go](examples/chapter_7/pointer_array.go): f(ar) // passes a copy of ar fp(&ar) // passes a pointer to ar } - + 输出结果: [0 0 0] @@ -119,13 +120,15 @@ Example 7.2 [pointer_array.go](examples/chapter_7/pointer_array.go): **练习** 练习7.1:array_value.go: 证明当数组赋值时,发生了数组内存拷贝。 + 练习7.2:for_array.go: 写一个循环并用下标给数组赋值(从0到15)并且将数组打印在屏幕上。 + 练习7.3:fibonacci_array.go: 在6.6节我们看到了一个递归计算Fibonacci数值的方法。但是通过数组我们可以更快的计算出Fibonacci数。完成该方法并打印出前50个Fibonacci数字。 ##7.1.2 数组常量 如果数组值已经提前知道了,那么可以通过`数组常量`的方法来初始化数组,而不用依次使用[]=方法。(所有的组成元素都有相同的常量语法) -Example 7.3 [array_literals.go][examples/chapter_7/array_literals.go] +Example 7.3 [array_literals.go](examples/chapter_7/array_literals.go) package main import "fmt" @@ -166,7 +169,7 @@ Example 7.3 [array_literals.go][examples/chapter_7/array_literals.go] 你可以取任意数组常量的地址来作为指向新实例的指针。 -Example 7.3 [pointer_array2.go][examples/chapter_7/pointer_array2.go] +Example 7.4 [pointer_array2.go](examples/chapter_7/pointer_array2.go) package main import "fmt" @@ -195,7 +198,7 @@ Example 7.3 [pointer_array2.go][examples/chapter_7/pointer_array2.go] 内部数组总是长度相同的。Go语言的多维数组是矩形式的(唯一的例外是分片slice的数组,参见7.2.5) -Example 7.5 [multidim_array.go][examples/chapter_7/multidim_array.go] +Example 7.5 [multidim_array.go](examples/chapter_7/multidim_array.go) package main const ( @@ -221,7 +224,7 @@ Example 7.5 [multidim_array.go][examples/chapter_7/multidim_array.go] 接下来的例子阐明了第一种方法: -Example 7.6 [array_sum.go][examples/chapter_7/array_sum.go] +Example 7.6 [array_sum.go](examples/chapter_7/array_sum.go) package main import "fmt" @@ -243,7 +246,7 @@ Example 7.6 [array_sum.go][examples/chapter_7/array_sum.go] 输出结果: The sum of the array is: 24.600000 -但这在Go中并不常用,通常使用分片slice。(参考[第7章](07.2.md) +但这在Go中并不常用,通常使用分片slice。(参考[第7章](07.2.md)) ##链接 - [目录](directory.md) diff --git a/eBook/images/7.1_fig7.1.png b/eBook/images/7.1_fig7.1.png index f34140e..292bd47 100644 Binary files a/eBook/images/7.1_fig7.1.png and b/eBook/images/7.1_fig7.1.png differ