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,这是一种变长的数组。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,640评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,254评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,011评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,755评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,774评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,610评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,352评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,257评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,717评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,894评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,021评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,735评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,354评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,936评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,054评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,224评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,974评论 2 355

推荐阅读更多精彩内容

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