Ruby 的數(shù)組
在之前的章節(jié)中,我們學(xué)習(xí)了布爾對(duì)象、數(shù)字對(duì)象、字符串對(duì)象等,但是有的時(shí)候,我們需要將一組對(duì)象組合起來(lái)成為一個(gè)獨(dú)立的對(duì)象,本章節(jié)讓我們將學(xué)習(xí) Ruby 數(shù)組,如何創(chuàng)建一個(gè)數(shù)組以及掌握其常用的實(shí)例方法。
1. 數(shù)組是什么?
Ruby 數(shù)組是任何對(duì)象的有序整數(shù)索引集合。數(shù)組中的每個(gè)元素都與一個(gè)索引相關(guān),并可通過(guò)索引進(jìn)行獲取?!?官方定義
Ruby 數(shù)組是一個(gè)包含許多元素的對(duì)象。這些元素可以是變量(例如 String,Integer,Hash 等),甚至可以是其他對(duì)象(包括構(gòu)成多維數(shù)組的其他數(shù)組)。定義中索引指的是數(shù)組元素中的一個(gè)序號(hào),它從0開(kāi)始,每個(gè)索引對(duì)應(yīng)一個(gè)元素。將所有元素放到數(shù)組之后,您可以通過(guò)各種方法來(lái)對(duì)數(shù)組之中的元素進(jìn)行操作。
那么如何創(chuàng)建一個(gè)數(shù)組呢?
2. 如何創(chuàng)建一個(gè)數(shù)組
Ruby 提供了許多創(chuàng)建數(shù)組的方式,數(shù)組本質(zhì)上是 Array 類,所以我們可以使用 Array 的new
方法來(lái)創(chuàng)建數(shù)組。
實(shí)例:
> Array.new
=> []
[]
表示一個(gè)空數(shù)組。我們還可以通過(guò)向new
方法傳遞參數(shù)預(yù)設(shè)數(shù)組的元素?cái)?shù)量。
實(shí)例:
# 預(yù)設(shè)里面有3個(gè)元素
> Array.new(3)
=> [nil, nil, nil]
解釋:通過(guò)預(yù)設(shè)大小之后的數(shù)組的元素為預(yù)設(shè)數(shù)量,所有元素會(huì)使用nil
代替。
除此之外,我們還可以在創(chuàng)建的時(shí)候預(yù)設(shè)數(shù)據(jù)。
實(shí)例:
> Array[ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
=> ["Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun"]
通常我們省略 Array,只使用方括號(hào)來(lái)預(yù)設(shè)數(shù)據(jù)。
注意事項(xiàng):Array.new()
參數(shù)是數(shù)字,用于預(yù)設(shè)數(shù)組的數(shù)量,而數(shù)組的每一個(gè)值都為nil
,而Array[]
中,參數(shù)是一組由逗號(hào)分隔的數(shù)據(jù),參數(shù)里面數(shù)據(jù)的數(shù)量即為數(shù)組數(shù)據(jù)的數(shù)量,值及為預(yù)設(shè)的值。
實(shí)例:
> [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
=> ["Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun"]
經(jīng)驗(yàn):我們有幾種快速創(chuàng)建數(shù)組的方式:
創(chuàng)建字符串?dāng)?shù)組:
> %w{ cat dog monkey }
=> ["cat", "dog", "monkey"]
創(chuàng)建 Symbol 數(shù)組:
> %i{ cat dog monkey }
=> [:cat, :dog, :monkey]
3. 數(shù)組的常用實(shí)例方法
3.1 判斷數(shù)組是否為空
使用empty?
方法來(lái)判斷數(shù)組是否為空:
實(shí)例:
# 定義一個(gè)空數(shù)組
> days_of_week = []
=> []
days_of_week.empty?
=> true
或者使用size
或者length
方法,判斷數(shù)組的長(zhǎng)度為0。
實(shí)例:
> days_of_week.length == 0
=> true
> days_of_week.size == 0
=> true
3.2 訪問(wèn)數(shù)組元素
數(shù)組的元素可以通過(guò)結(jié)合[]
方法引用相關(guān)元素的索引來(lái)訪問(wèn)。例如,要訪問(wèn)數(shù)組的第一個(gè)和第二個(gè)元素。
實(shí)例:
# 定義一個(gè)數(shù)組
> days_of_week = [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
=> [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
> days_of_week[0]
=> "Mon"
> days_of_week[1]
=> "Tues"
或者使用at
方法:
實(shí)例:
> days_of_week.at(0)
=> "Mon"
可以使用 Array 類的first
和last
方法訪問(wèn)首個(gè)和末尾元素。
實(shí)例:
> days_of_week.first
=> "Mon"
> days_of_week.last
=> "Sun"
3.3 查找某一個(gè)元素的索引
通常在使用數(shù)組時(shí),有必要找出特定元素的索引。這可以通過(guò)使用 index
方法來(lái)實(shí)現(xiàn),該方法返回第一個(gè)元素的索引以匹配指定的條件。例如,確定星期幾數(shù)組中“星期三”元素的索引值。
實(shí)例:
# 定義一個(gè)數(shù)組
> days_of_week = [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
=> [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
> days_of_week.index("Wed")
=> 2
rindex
方法可用于查找數(shù)組中的最后一個(gè)匹配元素。
實(shí)例:
# 定義一個(gè)數(shù)組
> days_of_week = ["today", "today", "today", "today", "today", "today", "today"]
=> ["today", "today", "today", "today", "today", "today", "today"]
# 返回首個(gè)匹配的索引
> days_of_week.index("today")
=> 0
# 返回最后匹配的索引
> days_of_week.rindex("today")
=> 6
3.4 提取數(shù)組元素的子集
可以通過(guò)指定起點(diǎn)和要提取的元素?cái)?shù)來(lái)提取數(shù)組元素的子集。
例如,從元素1開(kāi)始并讀取3個(gè)元素:
實(shí)例:
# 定義一個(gè)數(shù)組
> days_of_week = [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
=> [ "Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun" ]
> days_of_week[1, 3]
=> ["Tues", "Wed", "Thu"]
同樣,我們可以指定一個(gè)范圍(請(qǐng)參考 Ruby的范圍章節(jié))。
實(shí)例:
> days_of_week[1..3]
=> ["Tues", "Wed", "Thu"]
或者,可以使用 Array 類的slice
方法。
實(shí)例:
> days_of_week.slice(1..3)
=> ["Tues", "Wed", "Thu"]
3.5 合并數(shù)組
Ruby 中的數(shù)組可以使用多種不同的方法進(jìn)行連接。
通過(guò)+
合并。
實(shí)例:
days1 = ["Mon", "Tue", "Wed"]
days2 = ["Thu", "Fri", "Sat", "Sun"]
days = days1 + days2
=> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
使用concat
方法
實(shí)例:
days1 = ["Mon", "Tue", "Wed"]
days2 = ["Thu", "Fri", "Sat", "Sun"]
days = days1.concat(days2)
=> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
使用<<
方法將元素附加到現(xiàn)有數(shù)組。
實(shí)例:
days1 = ["Mon", "Tue", "Wed"]
days1 << "Thu" << "Fri" << "Sat" << "Sun"
=> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
3.6 交集、并集、差集
方法 | |
---|---|
- | 差集:返回一個(gè)新數(shù)組,該數(shù)組是第一個(gè)數(shù)組的副本,同時(shí)也刪除了第二個(gè)數(shù)組中出現(xiàn)的所有項(xiàng)目。 |
& | 交集:從兩個(gè)現(xiàn)有數(shù)組創(chuàng)建一個(gè)新數(shù)組,該數(shù)組僅包含兩個(gè)數(shù)組共有的元素,重復(fù)項(xiàng)會(huì)被刪除。 |
| | 并集:連接兩個(gè)數(shù)組,重復(fù)項(xiàng)會(huì)被刪除。 |
實(shí)例:
讓我們先定義兩個(gè)數(shù)組:
operating_systems = ["Fedora", "SuSE", "RHEL", "Windows", "MacOS"]
linux_systems = ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
創(chuàng)建一個(gè)并集,并集操作將一個(gè)數(shù)組連接到另一個(gè)數(shù)組,但是刪除了所有重復(fù)的數(shù)組元素。
operating_systems | linux_systems
=> ["Fedora", "SuSE", "RHEL", "Windows", "MacOS", "PCLinuxOS", "Ubuntu"]
接下來(lái)創(chuàng)建一個(gè)交集,獲得兩個(gè)數(shù)組共有的元素。
operating_systems & linux_systems
=> ["Fedora", "SuSE", "RHEL"]
最后我們創(chuàng)建差集,我們不只是使用此運(yùn)算符刪除重復(fù)的條目,還需要從左側(cè)操作數(shù)指定的數(shù)組中刪除在右側(cè)操作數(shù)指定的數(shù)組中也存在的項(xiàng)。
operating_systems - linux_systems
=> ["Windows", "MacOS"]
linux_systems - operating_systems
=> ["PCLinuxOS", "Ubuntu"]
3.7 去除數(shù)組元素中重復(fù)項(xiàng)
使用uniq
方法刪除重復(fù)項(xiàng)。
linux_systems = ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora", "RHEL", "SuSE"]
linux_systems.uniq
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
注意事項(xiàng):在這種情況下,uniq
方法不會(huì)更改原始數(shù)組。如果您真的想從數(shù)組中刪除重復(fù)項(xiàng),以便更改數(shù)組本身,請(qǐng)使用uniq!
實(shí)例:
linux_systems
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora", "RHEL", "SuSE"]
linux_systems.uniq!
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
linux_systems
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
3.8 向數(shù)組中增加或減少元素(push和pop)
Ruby 中的數(shù)組可以視為“后進(jìn)先出”堆棧。當(dāng)我們想要將新元素放到數(shù)組中,它會(huì)被插入到最后一個(gè)。
我們使用push
方法。
實(shí)例:
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
colors.push "indigo"
=> ["red", "green", "blue", "indigo"]
colors.push "violet"
=> ["red", "green", "blue", "indigo", "violet"]
我們使用pop
方法從數(shù)組移出元素,數(shù)組會(huì)從最后一個(gè)元素開(kāi)始移出。pop
的返回值是被移出的元素。
實(shí)例:
colors.pop
=> "violet"
colors.pop
=> "indigo"
3.9 數(shù)組的比較
可以使用==
,<=>
和eql?
比較 Ruby 數(shù)組。
如果兩個(gè)數(shù)組包含相同數(shù)量的元素并且每個(gè)數(shù)組的內(nèi)容相同,則==
返回true
。
在==
為true
的基礎(chǔ)上,每個(gè)元素的元素類型也相同,則eql?
返回true
。
最后,<=>
方法(也稱為“飛船”方法)比較兩個(gè)數(shù)組,如果兩個(gè)數(shù)組相等(此處比較方式同==
),則返回0;如果元素小于另一個(gè)數(shù)組中的元素,則返回-1;如果它們大于另一個(gè),則返回1。
實(shí)例:
a = [1, 'a', 1.0]
b = [1.0, 'a', 1.0]
a == b # true
a.eql?b # false
a <=> b # 0
3.10 修改數(shù)組元素
使用insert
方法在數(shù)組指定索引位置插入元素。
實(shí)例:
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
colors.insert( 1, "orange" )
=> ["red", "orange", "green", "blue"]
使用[]=
方法,修改數(shù)組指定索引位置的值。
實(shí)例:
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
colors[1] = "yellow"
=> "yellow"
colors
=> ["red", "yellow", "blue"]
在[]=
方法中使用范圍,可以一次修改多個(gè)元素。
實(shí)例:
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
colors[1..2] = "orange", "pink"
=> ["orange", "pink"]
colors
=> ["red", "orange", "pink"]
3.11 刪除數(shù)組元素
基于索引來(lái)刪除元素,我們使用delete_at
方法。
實(shí)例:
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
colors.delete_at(1)
=> "green"
colors
=> ["red", "blue"]
基于元素內(nèi)容來(lái)刪除的話,使用delete
方法。
實(shí)例:
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
colors.delete("red")
=> "red"
colors
=> ["green", "blue"]
3.12 數(shù)組的排序
numbers = [1, 4, 6, 7, 3, 2, 5]
=> [1, 4, 6, 7, 3, 2, 5]
numbers.sort
=> [1, 2, 3, 4, 5, 6, 7]
reverse
方法可以將數(shù)組倒序排列。
numbers = [1, 4, 6, 7, 3, 2, 5]
=> [1, 4, 6, 7, 3, 2, 5]
numbers.reverse
=> [5, 2, 3, 7, 6, 4, 1]
Tips : 和
uniq!
方法一樣,使用sort!
和reverse!
方法會(huì)對(duì)調(diào)用方法的數(shù)組直接進(jìn)行修改。
4. 小結(jié)
在本章中,我們學(xué)習(xí)了如何在 Ruby 中創(chuàng)建數(shù)組,如何對(duì)數(shù)組進(jìn)行為空判斷、如何訪問(wèn)數(shù)組元素、查找索引、提取元素子集、合并數(shù)組、去重、排序、刪除某一元素以及進(jìn)行交集、并集、差集操作。