Ruby 学习(六)数组Array

数组 []

基础

  • 数组通过[]索引访问
  • 通过赋值操作插入、删除、替换元素
  • 通过+号进行合并和删除元素,且集合做为新集合出现
  • 通过<<号向原数据追加元素
  • 通过*号重复数组元素
  • 通过&符号做并集和交集操作(注意顺序)

索引

  • 从 0 开始
  • 负数的索引是从尾部开始计数的,索引为 -1 表示数组的最后一个元素

常用API

创建

初始化一个空数组

names = Array.new

指定的值初始化

nums = Array[1, 2, 3, 4,5]

数组的大小 size length


例子

names = Array.new
puts "#{names.size}" 
puts "#{names.length}"

输出

0
0

判断是否为空 array.empty?

  • true: 空数组
  • false: 非空数组
names = Array["one", "two", "three", "four"]
puts "#{names.empty?}" # false
names.clear
puts "#{names.empty?}" # true

&

返回一个新的数组,包含两个数组中共同的元素,没有重复。

names = Array["one", "two", "three", "four"]
names2 = Array["three", "four", "five"]

array = names & names2
puts array

输出

three
four

*

array * int

数组重复 int

names = Array["one", "two", "three", "four"]

array = names * 2
puts array

输出

one
two
three
four
one
two
three
four

array * str

数组中的元素通过 str 拼接

names = Array["one", "two", "three", "four"]

array = names * '-'
puts array

输出

one-two-three-four

+

两个数组进行拼接

names = Array["one", "two", "three", "four"]
names2 = Array["three", "four", "five"]

array = names + names2
puts array

输出

one
two
three
four
three
four
five

-

A - B

从数组 A 中 移除在数组 B 中出现的元素

例子

names = Array["one", "two", "three", "four"]
names2 = Array["three", "four", "five"]

array = names - names2
puts array

输出

one
two

|

A | B

把数组B中的元素添加到A中,但是会移除重复的元素

例子

names = Array["one", "two", "three", "four"]
names2 = Array["three", "four", "five"]

array = names | names2
puts array

输出

one
two
three
four
five

<<

把元素添加到数组的末尾

例子

names = Array["one", "two", "three", "four"]

array = names << "new"
puts array

输出

one
two
three
four
new

==

数组相等的判断

获取元素

array[index]

  • 获取 index 位置处元素
  • 超过范围会为空
  • 负数表示从末尾开始取元素
names = Array["one", "two", "three", "four"]

puts names[0]  # one
puts names[2]  # three
puts names[3]  # four
puts names[4]  # 
puts names[-1] # four
puts names[-2] # three

array[start, length]

  • start 位置开始(包含这个位置),获取 length 个元素
  • 超过范围会只返回数组最大元素
names = Array["one", "two", "three", "four"]

puts "1 ====="
puts names[0, 1]  
puts "2 ====="
puts names[0, 2]  
puts "3 ====="
puts names[0, 3]  
puts "4 ====="
puts names[0, 4]  
puts "5 ====="
puts names[0, 5] 

输出

1 =====
one
2 =====
one
two
3 =====
one
two
three
4 =====
one
two
three
four
5 =====
one
two
three
four

array[range]

获取 range 范围内的元素

names = Array["one", "two", "three", "four"]

puts names[0..2]  

输出

one
two
three

array.at(index)

等价于array[index]

names = Array["one", "two", "three", "four"]

puts names.at(2)
three

array.slice(index)

array.slice(start, length)

array.slice(range)

array.fetch(index)

  • 获取 index 位置处的元素,
  • 如果index超过索引范围,会抛出异常 IndexError

names = Array["one", "two", "three", "four"]
puts "#{names.fetch(0)}"
one

names = Array["one", "two", "three", "four"]
names.fetch(10)
/Users/ocean/Desktop/code/ruby/learn/one/one.rb:2:in `fetch': index 10 outside of array bounds: -4...4 (IndexError)

array.fetch(index, default)

  • 获取 index 位置处的元素,
  • 如果index超过索引范围,会返回默认值 default
names = Array["one", "two", "three", "four"]
puts "#{names.fetch(10, "default")}"
default

array.fetch(index) { |index| block }

  • 获取 index 位置处的元素,
  • 如果index超过索引范围,会调用block
names = Array["one", "two", "three", "four"]
res = names.fetch(10) do |index|
  return  "#{index} new"
end
print res

array.first

  • 获取第一个元素
  • 如果数组为空,返回 nil
names = Array["one", "two", "three", "four"]
print names.first
one

array.first(n)

  • 获取前 n 个元素
names = Array["one", "two", "three", "four"]
print names.first(3)
["one", "two", "three"]

array.last

  • 获取最后一个元素
  • 如果数组为空,返回 nil
names = Array["one", "two", "three", "four"]
puts names.last
four

array.last(n)

  • 获取后n个元素
names = Array["one", "two", "three", "four"]
puts names.last(2)
three
four

添加元素

array.concat(other_array)

把数组other_array中的元素追加到数组array

names = Array["one", "two", "three", "four"]
names2 = Array["new", "12"]
puts names.concat(names2)
one
two
three
four
new
12

插入元素 array.insert(index, obj...)

  • 在给定的 index 的元素前插入给定的值
  • index 可以是负值。
names = Array["one", "two", "three", "four"]
names.insert(2, "new")

puts names
one
two
new
three
four

array.push(obj, ...)

在末尾添加元素 obj

names = Array["one", "two", "three", "four"]
names.push("tem")
puts names
one
two
three
four
tem

修改元素

array[index] = obj

修改index位置处的元素为obj

names = Array["one", "two", "three", "four"]
names[1] = "new"
puts names 

输出

one
new
three
four

array[start, length] = obj or an_array or nil

  • start 开始 length长度的元素替换为 元素obj,或者数组an_array,或者空nil

names = Array["one", "two", "three", "four"]
names[0,2] = "new"
puts names 

输出

new
three
four

names = Array["one", "two", "three", "four"]
names[0,2] = Array["new1", "new2", "new3", "new4"]
puts names 

输出

new1
new2
new3
new4
three
four

array[range] = obj or an_array or nil

删除元素

array.clear

移除所有的元素

names = Array["one", "two", "three", "four"]
names.clear
puts names

array.compact

移除了所有的nil元素

array.delete(obj) array.delete(obj) { block }

删除指定的元素

names = Array["one", "two", "three", "four"]
names.delete("two")
puts names
one
three
four

array.delete_at(index)

删除指定位置处的元素,如果超过数组大小,返回nil

names = Array["one", "two", "three", "four"]
names.delete_at(3)
puts names
one
two
three

array.delete_if { |item| block }

删除满足条件的元素

names = Array["one", "two", "three", "four"]
names.delete_if { |item| item == "two" }
puts names
one
three
four

array.shift

删除第一个元素

names = Array["one", "two", "three", "four"]
names.shift
puts names
two
three
four

array.pop

删除最后一个元素

names = Array["one", "two", "three", "four"]
names.pop
puts names
one
two
three

查找

包含元素 array.include?(obj)

names = Array["one", "two", "three", "four"]
print names.include?("two") # true
print names.include?("ten") # false

元素的索引 array.index(obj)

  • 获取元素的索引
  • 没有找到返回nil
names = Array["one", "two", "three", "four"]
puts "#{names.index("two")}" # 1
puts "#{names.index("ten")}" # nil

最后一个元素的索引 array.rindex(obj)

names = Array["one", "two", "three", "two"]
puts "#{names.index("two")}" # 1
puts "#{names.rindex("two")}" # 3

array.replace(other_array)

数组替换为其他数组

names = Array["one", "two", "three", "four"]
names2 = Array["ten", "new"]
names.replace(names2)
puts names
ten
new

遍历

array.map { |item| block }

转换为新一个数组

names = Array["one", "two", "three", "four"]
array = names.map{ |i| "#{i}" + "_new" }
puts array

输出

one_new
two_new
three_new
four_new

array.collect { |item| block }

转换为一个新的数组

array.each { |item| block }

遍历元素,传递的是元素

names = Array["one", "two", "three", "four"]
names.each do |item|
    puts "元素: #{item}"
end
元素: one
元素: two
元素: three
元素: four

array.each_index { |index| block }

遍历元素,传递的是索引

names = Array["one", "two", "three", "four"]
names.each_index do |idx|
  puts "idx: #{idx}, value: #{names[idx]}"
end
idx: 0, value: one
idx: 1, value: two
idx: 2, value: three
idx: 3, value: four

array.select {|item| block }

查找 block 返回true的元素

names = Array["one", "two", "three", "two"]
res = names.select { |item|
  item.include?("o")
}
puts res
one
two
two

删除重复的元素 array.uniq

names = Array["one", "two", "three", "four", "one"]
puts names.uniq
one
two
three
four

元素拼接 array.join(sep=$,)

names = Array["one", "two", "three", "four"]
puts names.join("--")
one--two--three--four

反转 array.reverse

names = Array["one", "two", "three", "four"]
puts names.reverse
four
three
two
one

排序

array.sort

names = Array["one", "two", "three", "four"]
puts names.sort
four
one
three
two

array.sort { | a,b | block }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值