我正在经历railstutorial,看到一个班轮以下红宝石oneliner VS常规
('a'..'z').to_a.shuffle[0..7].join
它创建随机7个字符的域名类似以下内容:
hwpcbmze.heroku.com
seyjhflo.heroku.com
jhyicevg.heroku.com
我试图转换一个衬垫的Groovy但我只能拿出:
def range = ('a'..'z')
def tempList = new ArrayList (range)
Collections.shuffle(tempList)
println tempList[0..7].join()+".heroku.com"
以上可以改善并作出一个班轮?我试图通过
println Collections.shuffle(new ArrayList (('a'..'z')))[0..7].join()+".heroku.com"
然而,为了使上面的代码更短,显然Collections.shuffle(new ArrayList (('a'..'z')))
是null
好...略短:。( 'A' .. 'Z')toList(){排序Math.random()} [1..7] .join()+“。heroku.com” – mbrevoort 2009-12-29 07:37:23
请注意,在Java 7中,它们有时会失败,因为它们默认情况下会使用Timsort,并且预计会比较2值保持不变。 – 2013-10-31 10:23:31