标签:
list = [[‘保密‘,‘‘],[‘男‘,1],[‘女‘,0]]
引用
怎么创建与返回值是二维数组形式
不知道你想要怎样的答案
如果是一方法想要返回二维数组,方法的最后一行是那个数组就好了
def list
[[‘保密‘,‘‘],[‘男‘,1],[‘女‘,0]]
end
p [[‘保密‘,‘‘],[‘男‘,1],[‘女‘,0]]
#或者
puts [[‘保密‘,‘‘],[‘男‘,1],[‘女‘,0]].inspect
irb(main):010:0> [[1],2,3].delete([1])
=> [1]
irb(main):011:0> [[1],2,3].delete(2)
=> 2
irb(main):012:0>
根据索引删除数组:
irb(main):012:0> [[1],2,3].delete_at(2)
=> 3
irb(main):013:0> [[1],2,3].delete_at(0)
=> [1]
irb(main):014:0>
irb(main):021:0> [[1],2,3].insert(0, [‘aa‘])
=> ["aa", [1], 2, 3]
irb(main):022:0> [[1],2,3].insert(-1, [‘aa‘])
=> [[1], 2, 3, "aa"]
ruby中数组不用预分配空间,所以不用覆盖长度,超出长度索引,不抛出,返回nil
irb(main):023:0> [[1],2,3][100]
=> nil
a=[1,2,3,4,5]
b=[‘a’,’b’,’c’,’d’]
a.zip(b)
b.zip(a)
输出数组元素的一个逗号分隔的列表.您将能够得到一个字符串分隔的任何字符串
a.join(‘,’)
输出数组元素的一个逗号分隔的列表将能够得到一个字符串分隔的任何字符串,数组中的元素使用Array#join方法。
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
可以使用Array#<<的方法,Array#unshift方法,Array#push。
将元素追加早最后,array#unshift方法可以将元素添加到数组开头。
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]
方法可以被添加到的数组元素的结尾
A = [1,2,3,4,5]
A.Push(10)#=> [1,2,3,4,5,10]
在Array#shift的方法,可以检索的元素,从一开始的数组。
array#pop方法是可以检索的数组中的元素。提取值的数组移位的方法,数组#pop方法,数组中的元素的提取物中删除。
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]
提取一个子数组
你可以把部分的数组用array#slice的方法或Array#[]的方法。
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]
&
数组与,返回两数组的交集
[1,2] & [2,3] =>[2]
*
复制数组n次
[1,2]*2 => [1,2,1,2]
+
返回两数组的并集,但不排除重复元素
[1,2]+[2,3] =>[1,2,2,3]
<<
追加元素,但不排除重复元素
[1,2]<<[2,3] => [1,2,2,3]
|
追加元素,但排除重复元素
[1,2] | [2,3] => [1,2,3]
-
返回第一个数组与第二个数组不同的元素
[1,2]-[2,3] => [1]
<=>
比较数组
[1,2]<=>[2,3] =>flase
==
比较数组,若所有元素均相等时返回真
[1,2]==[2,1] =>flase
assoc
从数组的每个元素中寻找指定对象
[[1,2],[3,4]].assoc(2)=> [1,2]
at
找到数组的第N个元素
负数表示逆向查找
["a","b","c","d","e"].at(0) => "a"
["a","b","c","d","e"].at(-1) => "e"
clear
删除数组中的所有元素
["a","b","c","d","e"]. clear
collect
collect!
用一个过程块对数组的每个元素进行处理
["a","b","c","d"].collect {|x| x + "!" } => ["a!", "b!", "c!", "d!"]
compact
compact!
删除值为nil的元素后生成新数组并返回它
["a",nil,"b",nil,"c",nil].compact => ["a", "b", "c"]
delete
删除元素,如果元素重复,全部删除
a = [ "a", "b", "b", "b", "c" ]
a.delete("b")
puts a => ["a","c"]
delete_at
删除pos所指位置的元素并返回它。若pos超出数组范围则返回nil
a = %w( ant bat cat dog )
a.delete_at(2)
a=> ["ant", "bat", "dog"]
a.delete_at(99) => nil
delete_if
根据条件删除
a = [ "a", "b", "c" ]
a.delete_if {|x| x >= "b" } => ["a"]
each
对数组的每个元素按值进行迭代操作
查寻方法find,也有:
list = [‘a‘,‘b‘,‘c‘] list.find{|x| x==‘c‘}
a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " } => "a -- b -- c --"
each_index
对数组的每个元素按索引进行迭代操作
a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }=> "0 -- 1 -- 2 --"
empty?
判断数组是否为空,为空则返回真
[].empty?=> true
eql!
比较两数组是否相等
["a","b","c"].eql?(["a","b","c"]) => true
fill
填充数组
["a","b","c","d"].fill("x") => ["x","x","x","x"]
["a","b","c","d"].fill("z", 2, 2) => ["x", "x", "z", "z"]
first
返回数组的首元素。若没有首元素则返回nil
[ "q", "r", "s", "t" ].first => "q"
last
返回数组末尾的元素。若数组为空时,返回nil
["w","x","y","z"].last => "z"
include?
判断数组中是否包含元素
a = [ "a", "b", "c" ]
a.include?("b") => true
a.include?("z") => false
index
返回数组中第一个==val的元素的位置
a = [ "a", "b", "c" ]
a.index("b") => 1
a.index("z") => nil
indexes
以数组形式返回其索引值与各参数值相等的元素
a = [ "a", "b", "c", "d", "e", "f", "g" ]
a.indexes(0, 2, 4) => ["a", "c", "e"]
a.indexes( 2, 4, 12)=> [ "c", "e", nil]
insert
在索引为nth的元素前面插入第2参数以后的值
ary = %w(foo bar baz)
ary.insert 2,‘a‘,‘b‘
p ary => ["foo", "bar", "a", "b", "baz"]
join
将数组元素按一定的分隔符连接起来
[ "a", "b", "c" ].join=> "abc"
[ "a", "b", "c" ].join("-") => "a-b-c"
length
size
返回数组长度。若数组为空则返回0
[1,2,3].length => 3
[1,2,3].size => 3
nitems
返回非nil元素的个数
[ 1, nil, 3, nil, 5 ].nitems => 3
pop
删除末尾元素并返回它。若数组为空则返回nil
a = [ "a", "m", "z" ]
a.pop => "z"
p a => ["a", "m"]
push
添加新元素
["a","b"].push([‘1‘,‘2‘])=>["a", "b", ["1", "2"]]
遍历数组每个元素(元素必须是数组),匹配索引为1的值是否与查找的字符相等,返回第一个相等的元素
a = [[15,1], [25,2], [35,2]]
p a.rassoc(2) => [25, 2]
替换数组元素
a = ["a","b"]
a.replace(["x","y","z"])
p a =>["x", "y", "z"]
reverse!
将所有元素以逆序重新排列生成新数组并返回它
["a","b","c" ].reverse=> ["c", "b", "a"]
返回最后一个值相等的元素的索引值
a = [ "a","b","b","b","c"]
a.rindex("b") => 3
删除数组的首元素并返回它。剩余元素依次提前。若数组为空返回nil
args = ["-m","-q","filename"]
args.shift => "-m"
args => ["-q", "filename"]
sort!
从小到大排序
a = [ "d", "a", "e", "c", "b" ]
a.sort => ["a", "b", "c", "d", "e"]
uniq!
删除数组中的重复元素后生成新数组并返回它
a = [ "a", "a", "b", "b", "c" ]
a.uniq => ["a", "b", "c"]
在数组第一个元素前添加元素
a = [ "b", "c", "d" ]
a.unshift("a") => ["a", "b", "c", "d"]
将数组的所有元素连接成字符串
["a","e","i","o"].to_s => "aeio"
二维数组变一维
标签:
原文地址:http://www.cnblogs.com/lizunicon/p/4956203.html