码迷,mamicode.com
首页 > 编程语言 > 详细

Ruby求出数组中最小值及其下标

时间:2015-03-16 21:14:05      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

其实很简单

Ruby的Array类自带了min方法可以求出最小值,然后调用Array的index方法传入元素值就可以求出下标

 

1 a = [1, 2, 3, 4, 5, 6] 
2 theMin = a.min     #=> 1 
3 theIndex = a.index(a.min)     #=> 0

 

Ruby求出数组中最小值及其下标

标签:

原文地址:http://www.cnblogs.com/hitnoah/p/4342790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!