在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在程序中定义一个数组 在程序中定义一个数组,因为我们在下面说明。 fruits = ["apple", "orange", "lemon"] scores = [55, 49, 100, 150, 0] 如果你是指使用数组的数组#[]的方法,我们将指定数目的元素的数组作为参数。我单元号是从0开始。在上面的例子中,fruits[0]返回“apple”,scores[3]将返回150。 fruits = [3, ["apple", 250], ["orange", 400], ["lemon", 300]] p fruits[0] #=> 3 p fruits[1][1] #=> 250 p fruits[3][0] #=> "lemon" 输出数组元素的一个逗号分隔的列表 p ["apple", "orange", "lemon"].join(',') #=> "apple,orange,lemon" p ["apple", "orange", "lemon"].join('#') #=> "apple#orange#lemon" p [55, 49, 100, 100, 0].join(',') #=> "55,49,100,100,0" p [3, ["apple", 250], ["orange", 400]].join(',') #=> "3,apple,250,orange,400" 获取在数组中的元素数 p ["apple", "orange", "lemon"].size #=> 3 p ["apple", "orange", "lemon"].length #=> 3 p [55, 49, 100, 100, 0].size #=> 5 p [3, ["apple", 250], ["orange", 400]].size #=> 3 将元素添加到数组 A = [1,2,3,4,5]#=> [1,2,3,4,5] A << 99#=> [1,2,3,4,5,99] A.Unshift (99)#=> [99,1,2,3,4,5,99] Array#push方法可以被添加到的数组元素的结尾 A = [1,2,3,4,5] A.Push(10)#=> [1,2,3,4,5,10] 从开头或结尾的数组元素删除 a = [1,2,3,4,5] a.push(10) #=> [1, 2, 3, 4, 5, 10] p a.pop #=> 10 p a.pop #=> 5 p a #=> [1, 2, 3, 4] 提取一个子数组 a = [1,2,3,4,5] p a[0,2] #=> [1, 2] p a[1..3] #=> [2, 3, 4] p a.slice(0,2) #=> [1, 2] p a.slice(1..3) #=> [2, 3, 4]
array#slice! 方法会改变原来数组元素 a = [1,2,3,4,5] p a.slice!(0,2) #=> [1,2] p a #=> [3,4,5] p a.slice!(1,2) #=> [4,5] p a #=> [3] 填充任何数组的值 a = [1,2,3,4,5] a.fill(255, 2,2) #=> [1, 2, 255, 255, 5] a.fill(0, 1..2) #=> [1, 0, 0, 255, 5] 清空数组 a = [1,2,3,4,5] a.clear #=> [] p a #=> [] 连接数组 可以使用array#+或者array#concat方法将多个数组连接 a = [1,2,3,4,5] p a + [10, 20] #=> [1, 2, 3, 4, 5, 10, 20] p a #=> [1, 2, 3, 4, 5] a.concat([10, 20]) #=> [1, 2, 3, 4, 5, 10, 20] p a #=> [1, 2, 3, 4, 5, 10, 20]
数组的交集与并集 p [1,3,5,7] | [2,4,6,8] #=> [1, 3, 5, 7, 2, 4, 6, 8] p [1,2,3,4] | [3,4,5,6] #=> [1, 2, 3, 4, 5, 6] p [1,3,5,7] & [2,4,6,8] #=> [] p [1,2,3,4] & [3,4,5,6] #=> [3, 4] 修改数组中多个元素 a = [1,2,3,4,5] a[0...2] = [111, 222, 333] p a #=> [111, 222, 333, 3, 4, 5] a[3,2] = [444,555] #=> [444, 555] p a #=> [111, 222, 333, 444, 555, 5] 将多维数组转换为一维数组 a = [1,[2,[3,4],5],[6,7]] p a.flatten #=> [1, 2, 3, 4, 5, 6, 7] a.flatten! #=> [1, 2, 3, 4, 5, 6, 7] 数组元素排序方法 p [5,1,4,2,3].sort #=> [1, 2, 3, 4, 5] a = [5,1,4,2,3] a.sort! #=> [1, 2, 3, 4, 5] p a #=> [1, 2, 3, 4, 5] p ["Orange", "Apple", "Lemon"].sort #=> ["Apple", "Lemon", "Orange"] 指定表达式的数组排序 a = ["Hitoshi,045", "Sizuo,046", "Yoshi,0138"] p a.sort{|a, b| a.split(',')[1] <=> b.split(',')[1]} #=> ["Yoshi,0138", "Hitoshi,045", "Sizuo,046"]
ar = [[2,"b"],[3,"a"],[1,"c"]] ar.sort{|p,q|p[0]<=>q[0]} #=> [[1, "c"], [2, "b"], [3, "a"]] ar.sort{|p,q|p[1]<=>q[1]} # => [[3, "a"], [2, "b"], [1, "c"]] 反转数组 a = [5,1,4,2,3] a.sort! #=> [1, 2, 3, 4, 5] p a.reverse #=> [5, 4, 3, 2, 1] a.reverse! #=> [5, 4, 3, 2, 1] 删除指定位置的数组元素 a = [5,1,4,2,3] a.delete_at(0) #=> 5 p a #=> [1, 4, 2, 3] a.delete_at(1) #=> 4 p a #=> [1, 2, 3] 删除匹配元素 a = ["apple", "orange", "lemon", "apple", "vine"] a.delete("apple") #=> ["apple"] p a #=> ["orange", "lemon", "vine"] a.delete("apple") { |x| puts "#{x} not found" } #=> "apple not found" 删除重复元素 a = [30,20,50,30,10,10,40,50] p a.uniq #=> [30, 20, 50, 10, 40] a = ["/tmp","/home/","/etc","/tmp"] a.uniq! #=> ["/tmp", "/home", "/etc"] 从数组中删除指定条件的元素 a = [30,100,50,80,79,40,95] a.delete_if {|x| x < 80} #=> [100, 80, 95] p a #=> [100, 80, 95] p a.reject {|x| x < 90} #=> [100, 95] p a #=> [100, 80, 95] a.reject! {|x| x < 90} #=> [100, 95] p a #=> [100, 95] a = ["Takeuchi", "Tanaka", "Satou", "Andou", "Kurasaki"] a.delete_if {|x| /^T.*/ =~ x} #=> ["Satou", "Andou", "Kurasaki"] 数组中提取指定条件的元素 a = [1, 2, 3, 4] p a.select{|x| x % 2 == 0} # => [2, 4] b = [['Taro', 'M'], ['Hanako', 'F'], ['Jiro', 'M'], ['Sachiko', 'F']] p b.select{|x| x[1]=='M'} # => [["Taro", "M"], ["Jiro", "M"]] 查找数组元素 a = ["apple",10,"orange",["lemon","vine"]] p a.index("apple") #=> 0 p a.index(10) #=> 1 p a.index(["lemon","vine"]) #=> 3 p a.index("fruit") #=> nil 搜索多维数组 a = [["apple",100],["vine",500],["orange",300]] p a.assoc("apple") #=> ["apple", 100] p a.assoc("orange") #=> ["orange", 300] p a.assoc("pear") #=> nil 创建新数组 使用array#map 或者array#collect方法 a = [10, 20, 30, 40, 50] p a.collect {|x| x*10} #=> [100, 200, 300, 400, 500] p a #=> [10, 20, 30, 40, 50] p a.map {|x| x*10} #=> [100, 200, 300, 400, 500] p a #=> [10, 20, 30, 40, 50] a.collect! {|x| x/10} #=> [1, 2, 3, 4, 5] p a #=> [1, 2, 3, 4, 5] a = [["vine", 2, 500], ["orange", 3, 100], ["apple", 10, 50]] p a.map {|x| [x[0], x[1]*x[2]]} #=> [["vine", 1000], ["orange", 300], ["apple", 500]] 迭代数组中各个元素 ["Taro", "Hanako", "Ichiro"].each {|x| puts "Hello, #{x}"} #=> "Hello, Taro" #=> "Hello, Hanako" #=> "Hello, Ichiro" sum = 0 [1,2,3,4,5,6,7,8,9,10].each {|x| sum += x} p sum #=> 55 还可以使用Array#reverse_each方法反转元素迭代 随机提取数组元素 if RUBY_VERSION < '1.9.0' class Array def choice at( rand( size ) ) end end end [1, 2, 3].choice #=> 3 [1, 2, 3].choice #=> 1
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论