Swift Review#5[複雜型態的資料儲存-元組Tuples]

影山小麥機
Jun 27, 2021

前言

在前兩篇複習了陣列(Array)、集合(Set)兩個概念之後,分別知道:

  1. 陣列是一種「有序」、「可索引」的資料儲存方式、可重複的

2.而集合則是「無序」、「不可以索引」的、不可重複的

兩者幾乎在功能上是相反的,所以在使用上也應該擇其優點來作為應用。那接下來會介紹新的資料儲存方式:Tuple

正文

引述 100 Day of Swift 對於元組概念的解析:

Both tuples and arrays allow us to hold several values in one variable, but tuples hold a fixed set of things that can’t be changed, whereas variable arrays can have items added to them indefinitely.

元組的概念是:“內容是不可變的,這是它跟陣列不相同之處。”

瞭解完元組的特性之後,我們可以在下面這裡知道,元組的表達方式是()小括弧:

然後如果要呼叫元組內的資料的話,可以直接指名參數,它就會對應內容出來,或者也可以以項目排序索引:

所以經上述的描繪,陣列、集合、元組三者可以放在一起做一些小比較:

  1. arrays keep the order(有序) and can have duplicates(重複項),
  2. sets are unordered(無序的) and can’t have duplicates(重複項),

tuples have fixed values(固定值) of fixed types(固定型別) inside them.

稍微簡述一下陣列、集合、元組三者的區別,可大致有下面的分類

1.陣列:有序、可目錄索引、可以有重複項

2.集合:無序、不可目錄索引,但可以詢問「存在或不存在」、不可重複

3.元組:固定值與固定型別、可目錄索引

基於上述三者的複習,可以知道程式語言在資料儲存的多元性,每種語言其實都有幾種共通的儲存方式,但有時候在細節上是有差異的,但分清楚就會很好使用。

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

影山小麥機
影山小麥機

Written by 影山小麥機

本職為Mobile工程師,熱愛分享視野,也樂意站在ChatGPT的肩膀上。訂閱小麥機,收割技術、職涯、人生的難題。

No responses yet

Write a response