Go语言复合数据类型之数组

在学习复合数据类型之前,我们得知道复合数据类型是由基本数据类型组合而成,和Java一样,Go语言也有基本数据类型,不过Go语言的基础数据类型有点特殊,它们分别是整数、浮点数、复数、布尔值、字符串、常量这六大类。相比之下,Java的基础数据类型就显得非常细致,它们分别是:byte、int、short、long、char、float、double、boolean。

数组的概念

数组是具有固定长度且拥有零个或者多个类型元素的序列。由于数组的长度是固定的,所以在Go里面很少直接使用,使用更多的是slice。slice是长度可以增加和缩短的数组,我们可以这样理解。但是呢,在学习slice之前,我们必须先深刻理解数组。

实战化数组

里面涉及到for循环,其中的range用于 for 循环中迭代数组(array)、切片(slice)、通道(channel)或集合(map)的元素。在数组和切片中它返回元素的索引和索引对应的值,在集合中返回 key-value 对。

<pre class="public-DraftStyleDefault-pre" data-offset-key="9j80-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="31tu7" data-offset-key="9j80-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

package main
import "fmt" func main() { var a [3]int //3个整数的数组 fmt.Println(a[0]) //输出数组的第一个元素 fmt.Println(a[len(a)-1]) //输出数组的最后一个元素 //输出索引和元素 for i,v := range a{ fmt.Printf("%d %d\n",i,v) } //仅输出元素 for _,v :=range a{ fmt.Printf("%d\n",v) } }

</pre>

</pre>

大家可以注意到,我们刚才在上面的代码中只是声明了一个长度为三的整型数组,并没有进行数组的初始化。那之前在Go语言程序结构之声明里面我们学过,在Go语言里面,我们声明变量Go编译器会自动给变量赋予零值,整型的零值是0。因此,上面的代码打印出来的内容就可以证明这一点。

image

在数组的声明中,如果在[]里面我们没有指出数组的长度,那么数组的长度就会由编译器进行元素个数的推断,并赋值。

<pre class="public-DraftStyleDefault-pre" data-offset-key="1nk1d-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="31tu7" data-offset-key="1nk1d-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

q := [...]int{1,2,3} fmt.Printf("%T\n",q) //"[3]int"

</pre>

</pre>

数组的长度是数组的一部分,数组的长度必须是常量表达式,这也就是说,数组的长度必须在编译器就能确定。

定义一个拥有100个元素的数组r,除了最后一个元素值是-1外,该数组中的其他元素都是0;

<pre class="public-DraftStyleDefault-pre" data-offset-key="2i17b-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="31tu7" data-offset-key="2i17b-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

r := [...]int{99:-1} fmt.Println(r[99]) //-1

</pre>

</pre>

如果数组的元素是可比较的,那么这个数组也是可比较的。因此我们就可以使用==操作符来比较两个数组是否完全相同,使用!=操作符来进行不同的比较。

<pre class="public-DraftStyleDefault-pre" data-offset-key="6ig9h-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="31tu7" data-offset-key="6ig9h-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

a := [2]int{1,2} b := [...]int{1,2} c := [2]int{1,3} fmt.Println(a==b,a==c,b==c) // true false false d := [3]int{1,2} fmt.Println(a == d) //编译错误,无法比较

</pre>

</pre>

在Go语言中,Go把数组和其他类型的都看成值传递,而在其他语言中,数组是隐式地使用引用传递。接下来,我们显式地传递一个数组的指针给函数,这样在函数内部对数组进行的操作,都会反映到原始数组上面。

<pre class="public-DraftStyleDefault-pre" data-offset-key="pm4m-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="31tu7" data-offset-key="pm4m-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

func zero(ptr *[32]byte){ for i := range ptr{ ptr[i] = 0 } }

</pre>

</pre>

基于上面的程序,我们还可以对该程序进行改写,实现另一个版本的数组清零程序。

<pre class="public-DraftStyleDefault-pre" data-offset-key="20m1o-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="31tu7" data-offset-key="20m1o-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

func zero(ptr *[32]byte){ *ptr = [32]byte{} }

</pre>

</pre>

由于数组是定长的,因此使用数组的指针可以高效的修改数组中的元素,但是数组不能够添加或者删除元素。因此,在实际情况中,我们很少使用数组,我们更多的会使用slice,这是一种变长的数组。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数组是具有相同数据类型的数据项组成的一组长度固定的序列,数据项叫做数组的元素,数组的长度必须是非负整数的常量,长度...
    imsilence阅读 275评论 0 0
  • 数组 数组一般都是指定长度的某种类型 。由于长度无法变化,其实很少使用到。 数组能直接做==的检测判断。在调用函数...
    阿彪2020阅读 120评论 0 0
  • Go语言中有四种复合数据类型:数组,slice,map,结构体 数组和结构体都是聚合类型,长度固定。而slice和...
    陈卧虫阅读 1,145评论 0 0
  • 推荐一个学习golang相关知识的资料网站,十分齐全go学习网站[http://www.topgoer.com/]...
    bug去无踪阅读 1,378评论 0 2
  • Go 语言中包括以下内置基础类型: 布尔型:bool 整型: int64 int32 int16 int8 uin...
    wayyyy阅读 385评论 0 0