Created
February 18, 2013 16:12
-
-
Save aeg/4978491 to your computer and use it in GitHub Desktop.
リストの要素をランダムに並び替える
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Case #1 | |
// リストの要素をランダムに並び替える | |
def list1 = ['a', 'b', 'c', 'd'] | |
def list2 = [] | |
while(list1.size() > 0) { | |
index = new Random().nextInt(list1.size()) | |
list2.add(list1[index]) | |
list1.remove(index) | |
} | |
println list2 | |
// 結果 | |
// [a, b, d, c] | |
// Case #2 | |
// リストの要素をランダムに並び替える | |
list1 = ['a', 'b', 'c', 'd'] | |
Collections.shuffle(list1) | |
println list1 | |
// 結果 | |
// [d, c, b, a] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment