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