Created
August 24, 2013 11:12
-
-
Save TomckySan/6327537 to your computer and use it in GitHub Desktop.
Collections#shuffleメソッド
This file contains 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
import java.util.ArrayList; | |
import java.util.Collections; | |
public class RandomListSample { | |
public static void main(String[] args) { | |
RandomList taskList = new RandomList( | |
new RandomList("a", "b", "c"), | |
new RandomList(1, 2, 3, 4, 5), | |
new RandomList("太郎", "次郎") | |
); | |
for(Object item : taskList) { | |
System.out.println(item); | |
} | |
} | |
} | |
/** | |
* 要素がランダムに並び替えられたListを生成するクラス | |
*/ | |
class RandomList extends ArrayList<Object> { | |
// 可変長引数 | |
public RandomList(Object... elems) { | |
for(Object obj : elems) { | |
add(obj); | |
} | |
// shuffleメソッドでランダムに並び替え | |
Collections.shuffle(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment