Skip to content

Instantly share code, notes, and snippets.

@TomckySan
Created August 24, 2013 11:12
Show Gist options
  • Save TomckySan/6327537 to your computer and use it in GitHub Desktop.
Save TomckySan/6327537 to your computer and use it in GitHub Desktop.
Collections#shuffleメソッド
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