Ruby Array 创建数组
irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> b=["1",2,3.14]
=> ["1", 2, 3.14]
irb(main):003:0> c=Array.new
=> []
irb(main):004:0> d=Array.new(5)
=> [nil, nil, nil, nil, nil]
irb(main):005:0> e=Array.new(5,0)
=> [0, 0, 0, 0, 0]
irb(main):006:0> la=%w(Ruby Perl Python Scheme Pike REBOL)
=> ["Ruby", "Perl", "Python", "Scheme", "Pike", "REBOL"]
irb(main):007:0> li=%i(Ruby Perl Python Scheme Pike REBOL)
=> [:Ruby, :Perl, :Python, :Scheme, :Pike, :REBOL]
irb(main):008:0> color_table={black:"#000000",white:"#FFFFF"}
=> {:black=>"#000000", :white=>"#FFFFF"}
irb(main):009:0> color_table.to_a
=> [[:black, "#000000"], [:white, "#FFFFF"]]
irb(main):010:0> column="1 2 3 4 5 8 0".split
=> ["1", "2", "3", "4", "5", "8", "0"]
索引的使用
li[0]
=> :Ruby
irb(main):012:0> li[4]
=> :Pike
irb(main):013:0> a[1..2]
=> [2, 3]
irb(main):014:0> a[0...3]
=> [1, 2, 3]
irb(main):015:0> a[0..3]
=> [1, 2, 3]
irb(main):016:0> a[0..2]
=> [1, 2, 3]
irb(main):017:0> a[0,2]
=> [1, 2]
irb(main):018:0> a[0,3]
=> [1, 2, 3]
irb(main):019:0> a[-1]
=> 3
irb(main):020:0> a[-1,2]
=> [3]
irb(main):021:0> a[-2]
=> 2
irb(main):022:0> a[-3]
=> 1
irb(main):023:0> a[-4]
=> nil
irb(main):024:0> li
=> [:Ruby, :Perl, :Python, :Scheme, :Pike, :REBOL]
irb(main):025:0> li[0]=:china
=> :china
irb(main):026:0> li
=> [:china, :Perl, :Python, :Scheme, :Pike, :REBOL]
irb(main):027:0> li[2,0]=[:X,:Y]
=> [:X, :Y]
irb(main):028:0> li
=> [:china, :Perl, :X, :Y, :Python, :Scheme, :Pike, :REBOL]
将数组用作集合
&: 集合的交,|:集合的并,-:集合的差,+:集合的连接
irb(main):029:0> ary1="a b c".split
=> ["a", "b", "c"]
irb(main):030:0> ary2="b c d".split
=> ["b", "c", "d"]
irb(main):031:0> ary1 & ary2
=> ["b", "c"]
irb(main):032:0> ary1 | ary2
=> ["a", "b", "c", "d"]
irb(main):033:0> ary1 - ary2
=> ["a"]
irb(main):034:0> ary2- ary1
=> ["d"]
irb(main):035:0> ary1 + ary2
=> ["a", "b", "c", "b", "c", "d"]
数组用作栈、队列 栈
a=[1,3,2,4]
a.push 99 #a=[1,2,3,4,99]
a.pop #99 a=[1,2,3,4]
队列
a=[1,2,3,4]
a.push 99 #a=[1,2,3,4,99]
a.shift #1 a=[2,3,4,99]
数组的主要方法
irb(main):042:0> a
=> [2, 46, 7, 8]
irb(main):043:0> a.unshift 10
=> [10, 2, 46, 7, 8]
irb(main):044:0> a<<1000
&#61;> [10, 2, 46, 7, 8, 1000]
irb(main):045:0> a.push 1001
&#61;> [10, 2, 46, 7, 8, 1000, 1001]
irb(main):046:0> a.concat "16 15 11".split
&#61;> [10, 2, 46, 7, 8, 1000, 1001, "16", "15", "11"]
irb(main):047:0> a&#43; [111,112,113]
&#61;> [10, 2, 46, 7, 8, 1000, 1001, "16", "15", "11", 111, 112, 113]
irb(main):048:0> ary&#61;[13,nil,666,nil,777,nil,999,nil]
&#61;> [13, nil, 666, nil, 777, nil, 999, nil]
irb(main):049:0> ary.compact
&#61;> [13, 666, 777, 999]
irb(main):050:0> a.delete 111
&#61;> nil
irb(main):051:0> a
&#61;> [10, 2, 46, 7, 8, 1000, 1001, "16", "15", "11"]
irb(main):052:0> a.delete_at 2
&#61;> 46
irb(main):053:0> a
&#61;> [10, 2, 7, 8, 1000, 1001, "16", "15", "11"]
irb(main):054:0> a.delete_if{|item| item.class&#61;&#61;String}
&#61;> [10, 2, 7, 8, 1000, 1001]
irb(main):055:0> a.reject {|item| item>&#61;1000}
&#61;> [10, 2, 7, 8]
irb(main):056:0> a.slice 1,2
&#61;> [2, 7]
irb(main):057:0> d&#61;[1,1,1,3,4,6,6,8,9]
&#61;> [1, 1, 1, 3, 4, 6, 6, 8, 9]
#获取只出现一次的元素
irb(main):059:0> d.uniq
&#61;> [1, 3, 4, 6, 8, 9]
irb(main):060:0> d.shift
&#61;> 1
irb(main):061:0> d
&#61;> [1, 1, 3, 4, 6, 6, 8, 9]
irb(main):062:0> a.pop
&#61;> 1001
irb(main):063:0> a
&#61;> [10, 2, 7, 8, 1000]
collect,map,fill
irb(main):064:0> arr&#61;[6,4,3,6,9,8]
&#61;> [6, 4, 3, 6, 9, 8]
irb(main):065:0> arr.collect{|item| item**2}
&#61;> [36, 16, 9, 36, 81, 64]
irb(main):066:0> a.map{|item| item*2}
&#61;> [20, 4, 14, 16, 2000]
irb(main):067:0> arr.map{|item| item*2}
&#61;> [12, 8, 6, 12, 18, 16]
irb(main):068:0> x&#61;Array.new
&#61;> []
irb(main):071:0> x<<1
&#61;> [1]
irb(main):072:0> x<<111
&#61;> [1, 111]
irb(main):073:0> x<<222
&#61;> [1, 111, 222]
irb(main):074:0> x.fill(0) #将所有值替换为0
&#61;> [0, 0, 0]
irb(main):075:0> y&#61;[1,111,222]#
&#61;> [1, 111, 222]
irb(main):076:0> y.fill(-1,1) #将索引为1处的值替换为-1
&#61;> [1, -1, -1]
irb(main):077:0> y.fill 1000,1,1 #fill(value,begin,len)
&#61;> [1, 1000, -1]
flatten,sort,sort_by
irb(main):078:0> e&#61;[]
&#61;> []
irb(main):079:0> e<<[1,3]
&#61;> [[1, 3]]
irb(main):080:0> e<<[2,4]
&#61;> [[1, 3], [2, 4]]
irb(main):081:0> e<<[9,0]
&#61;> [[1, 3], [2, 4], [9, 0]]
irb(main):082:0> e.flatten
&#61;> [1, 3, 2, 4, 9, 0]
irb(main):083:0> e.reverse
&#61;> [[9, 0], [2, 4], [1, 3]]
irb(main):084:0> f&#61;e.flatten
&#61;> [1, 3, 2, 4, 9, 0]
irb(main):085:0> f.sort{|i,j| i<&#61;>j}
&#61;> [0, 1, 2, 3, 4, 9]
irb(main):086:0> f.sort_by{|item| -item}
&#61;> [9, 4, 3, 2, 1, 0]
获取数组的各元素值
list&#61;[1,3,5,7,9]
&#61;> [1, 3, 5, 7, 9]
irb(main):094:0> sum&#61;0
&#61;> 0
irb(main):095:0> list.each do |item|
irb(main):096:1* sum&#43;&#61;item
irb(main):097:1> end
&#61;> [1, 3, 5, 7, 9]
irb(main):098:0> sum
&#61;> 25
irb(main):099:0> list.each_with_index do |item,index|
irb(main):100:1* p item,index end
1
0
3
1
5
2
7
3
9
4
访问多个数组
irb(main):101:0> ary1&#61;[1,2,3]
&#61;> [1, 2, 3]
irb(main):102:0> ary2&#61;[100,200,300]
&#61;> [100, 200, 300]
irb(main):103:0> ary3&#61;[5,4,3]
&#61;> [5, 4, 3]
irb(main):104:0> ary1.zip ary2,ary3 do |a,b,c|
irb(main):105:1* p a.to_s&#43;" "&#43;b.to_s&#43;" "&#43;c.to_s end
"1 100 5"
"2 200 4"
"3 300 3"
&#61;> nil