Ruby 中数组的常用操作

来源:互联网 发布:美国黑人矛盾 知乎 编辑:程序博客网 时间:2024/04/29 13:34

企业级开发中很多时候,并不是去操作项目框架,更多的时候我们是了解业务逻辑然后去操作数据的增删该查。今天就Ruby 中操作数组的方法进行总结。感谢新浪小边边的博客

数组的创建和初始化

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] 初始大小3f = 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 #=>4y = [1,2,nil,nil,3,4]c = y.size  #=>6d = y.length #=>6e = y.nitems #=>4

数组比较

a = [1,2,3,9,9]b = [1,2,4,1,1]c = a <=> b #=> -1 意思是 ad = [1,2,3]e = [1,2,3,4]c = a <=> b #=> -1 意思是 a定义一个比较方法class Comparable,因为数组默认只有 <=> 现在通过自写class增加 < > <= >=class Comparabledef <(other)(self <=>other) == -1enddef <=(other)(self < other) or (self == other)enddef >(other)(self <=> other) == 1enddef >=(other)(self > other) or (self == other)endend然后就可以直接数组 a < b 返回true

数组排序

words = %w(the quick brown fox) #定义数组words.sort #=> ["brown","fox","quick","the"] #sort进行排序正常排序a.sort #=> ["1", "2", "3", "5", "6", "four", "three", "two"]另一方法比较排序 返回1a.sort{|x,y| x.to_s <=> y.to_s} #=> ["1", "2", "3", "5", "6", "four", "three", "two"]相反比较排序 返回-1a.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]查询最大最小值x.max#=>30 x.min#=>4a = %w[January February March April May] 新建一个数组正在查询a.grep(/ary/)#=> ["January", "February"]a.grep(/ary/){|i| i.length}#=> [7, 8] 查询出来的内容转换为个数.删除数组中的重复项 (uniq或uniq!)删除数组中为nil的元素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位,返回被删除对象,如果不存在返回nila#=> [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 Arraydef countk=Hash.new(0)self.each{|x| k[x]+=1}kendendmeal=%w[spam spam eggs ham eggs spam]items=meal.count#items is {"ham"=>1,"spam"=>3,"eggs"=>2}spams=items["spam"] #3

我在实际开发中能用上的估计就是这么多了,更多关于Ruby 数组的操作可以查看ruby 数组介绍更多链接(http://blog.sina.com.cn/s/blog_629b5cc50101etnj.html)

1 0
原创粉丝点击