Ruby教程之注释、变量声明以及数组操作
## 1、数组定义
arr1 = []
arr2 = Array.new
arr3 = ['1','2','3']
## 2、输出
puts "Output:
"
print arr3,"
" # 输出:["1", "2", "3"]
puts arr3[0] # 输出:1
print arr3.join(",") # 输出:1,2,3
## 3、访问数组元素
puts "
Array Element
"
puts "
", arr3[0]
puts "
", arr3[-1] #这个比较有意思,意思是最后一个的数据
puts "
", arr3.first
puts "
", arr3.last
puts "
", arr3.max
puts "
", arr3.min
puts "
", arr3.length
puts "
", arr3.size
## 4、数组操作
puts "
Array method
"
puts "
", arr3[4] = '5' #此时arr3[3]默认尾nil
puts "
", arr3.delete_at(3) #删除指定下标的元素
puts "
", arr3[2] = ["3a","3b","3c"] #可以这样来做
puts "
", arr3.delete(["3a","3b","3c"]) #可以删除上面添加的
puts "
", arr3.push('6') #插入元素
puts "
", arr3.insert(3,"b") #在指定位置插入新元素
puts "
", arr3<<"f"<<2 #从左至右一次加入元素
puts "
", arr3.pop #删除尾元素
puts "
", arr3.shift #删除首元素
puts "
", arr3.clear #清空数组
## 5、数组运算
puts "
Array Operator
"
aaa = [" aa ",4,5," bb "]
bbb = [4,1,3,2,5]
print aaa + bbb,"
" # 输出:[" aa ", 4, 5, " bb ", 4, 1, 3, 2, 5]
print aaa*2,"
" # 输出:[" aa ", 4, 5, " bb ", " aa ", 4, 5, " bb "]
print bbb - aaa,"
" # 输出:[1, 3, 2]
print aaa | bbb,"
" # 输出:[" aa ", 4, 5, " bb ", 1, 3, 2]
print aaa & bbb,"
" # 输出:[4, 5]
print bbb.sort,"
" # 输出:[1, 2, 3, 4, 5] 排序
print aaa.reverse,"
" # 输出:[" bb ", 5, 4, " aa "] 倒置
print bbb.sort.reverse,"
" # 输出:[5, 4, 3, 2, 1] 降序
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播