modify embrace

This commit is contained in:
chidouhu
2013-11-20 15:59:00 +08:00
parent 9fa4f00725
commit b8d6e4d469
2 changed files with 13 additions and 10 deletions

View File

@@ -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,7 +22,7 @@ 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
@@ -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) }
@@ -119,13 +120,15 @@ Example 7.2 [pointer_array.go](examples/chapter_7/pointer_array.go):
**练习**
练习7.1array_value.go: 证明当数组赋值时发生了数组内存拷贝
练习7.2for_array.go: 写一个循环并用下标给数组赋值从0到15并且将数组打印在屏幕上
练习7.3fibonacci_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)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 14 KiB