我正在經歷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