# Ruby 中数组的常用操作

2016-12-20 10:04:45来源:网络收集作者:码农伯伯人点击

a = Array.[](1,2,3,4)
b = Array[1,2,3,4]
c = [1,2,3,4]
d = Array.new #创建一个空数组
e = Array.new(3) #[nil,nil,nil] 初始大小3
f = Array.new(3,"xx") #["xx","xx","xx"] 初始大小3 初始值"xx"
f[0].capitalize! #=> ["Xx","Xx","Xx"]
g = Array.new(3){"yy"} #=> ["yy","yy","yy"]
g[0].capitalize! #=>["Yy","yy","yy"]

a = [1,2,3,4,5,6]
b = a[0] #=>1等同于c = a.at[0] #=>1 取出数组第1位的值
d = a[-2] #=>5等同于e = a.at[-2] #=>5 取出 倒数第2位
f = a[9] #=>nil 不存在第9位
g = a.at[9] #=>nil 当第9位不存在
h = a[3,3] #=>[4,5,6] 从第4位开始,一共3个
i = a[2..4] #=>[3,4,5] 第2位后 到第4位之间
j = a[2...4] #=>[3,4] 第2位后 到第4位之前(不包括4位)
h,i,j 这几个数组时间的差别很小，但是结果缺完全不一样。
a[1] = 8 #=> [1,8,3,4,5,6] 第2位被重新赋值
a[1,3] = [10,30,30] #=>[1,10,30,30,5,6] 从第2为开始替换数组 共3位
a[0..3] = [2,4,6,8] #=> [2,4,6,8,5,6] 从第1位开始到第4位 替换
a[-1] = 12 #=> [2,4,6,8,5,12] 替换最后一位
k = [2,4,6,8,10]
k[1..2] = [3,3,3] #=>[2,3,3,3,8,10] 替换第2位～第三位之间的
k[7] = 99 #=> [2,3,3,3,8,10,nil,99] 第8位增加99 第7位不存在
m = [1,3,5,7,9]
m[2] = [20,30] #=> [1,2,[20,30],7,9] 把第3位替换为此数组
m = [1,3,5,7,9]
m[2..2]=[20,30] #=> [1,2,20,30,7,9] 在第2位之间加入数组
x = [0,2,4,6,8,10,12]
a = x.slice(2) #=>4 取第3位
b = x.slice(2,4) #=>[4,6,8,10] 从第3位开始取 4个数
c = x.slice(2..4) #=>[4,6,8] 从第3位..到第5位
x.first #=>0 取第1位
x.last #=>12 取最后1位
x = [10,20,30,40,50,60]
y = x.values_at(0,1,4) #=>[10,20,50] 取出值在1位 2位 5位上的数

x = ["a","b","c","d"]
a = x.length #=>4 等同于 a = x.size #=>4
y = [1,2,nil,nil,3,4]
c = y.size#=>6
d = y.length #=>6
e = y.nitems #=>4

a = [1,2,3,9,9]
b = [1,2,4,1,1]
c = a <=> b #=> -1 意思是 a
d = [1,2,3]
e = [1,2,3,4]
c = a <=> b #=> -1 意思是 a

class Comparable
def <(other)
(self <=>other) == -1
end
def <=(other)
(self < other) or (self == other)
end
def >(other)
(self <=> other) == 1
end
def >=(other)
(self > other) or (self == other)
end
end

words = %w(the quick brown fox) #定义数组
words.sort #=> ["brown","fox","quick","the"] #sort进行排序

a.sort #=> ["1", "2", "3", "5", "6", "four", "three", "two"]

a.sort{|x,y| x.to_s <=> y.to_s} #=> ["1", "2", "3", "5", "6", "four", "three", "two"]

a.sort{|x,y| x.to_s <=> y.to_s} #=> ["two", "three", "four", "6", "5", "3", "2", "1"]

files.sort{|x,y| File.size(x) <=> File.size(y)}

files.sort_by{|x| File.size(x)}

lists.sort_by{|x| x.name,x.age,x.height}

x = [5,8,12,9,4,30]

a = %w[January February March April May] 新建一个数组

a.grep(/ary/)
#=> ["January", "February"]
a.grep(/ary/){|i| i.length}
#=> [7, 8] 查询出来的内容转换为个数.

a = [1,2,nil,3,nil,4,5]
#=> [1, 2, nil, 3, nil, 4, 5]
a.compact
#=> [1, 2, 3, 4, 5]
a.compact!
#=> [1, 2, 3, 4, 5]
a
#=> [1, 2, 3, 4, 5]

a = [10,12,14,16,18]
#=> [10, 12, 14, 16, 18]
a.delete_at(3)
#=> 16 #删除第4位,返回被删除对象,如果不存在返回nil
a
#=> [10, 12, 14, 18]
a.delete(10)
#=>10 #返回10

x=[1,5,9]
#=> [1, 5, 9]
x << 13
#=> [1, 5, 9, 13]
x << 16 << 18
#=> [1, 5, 9, 13, 16, 18]#主要时<< 这个符号加入附加
x=[1,2]
y=[3,4]
z=[5,6]
b=y+z
#=> [3, 4, 5, 6]
b +=x
#=> [3, 4, 5, 6, 1, 2]
z.concat y
#=> [5, 6, 3, 4]

words = %w(Son I am able she said)
#=> ["Son", "I", "am", "able", "she", "said"]
words.join(",")
#=> "Son,I,am,able,she,said"

a=[1,2,3]
#=> [1, 2, 3]
a.reverse
#=> [3, 2, 1]
a.reverse!
#=> [3, 2, 1]
a
#=> [3, 2, 1]

class Array
def count
k=Hash.new(0)
self.each{|x| k[x]+=1}
k
end
end
meal=%w[spam spam eggs ham eggs spam]
items=meal.count
#items is {"ham"=>1,"spam"=>3,"eggs"=>2}
spams=items["spam"] #3