java隨機分組

 1.利用Collections的shuffle把定義好的集合隨機打亂再按順序截出來

public class Waner {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        String[] arr = {"鐵皮", "救護車", "漂移", "十字線", "探長", "飛過天", "爵士", "紅蜘蛛", "震盪波", "擎天柱", "大黃蜂", "威震天", "熱破", "補天士", "通天曉", "猛大帥"};
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }
        Collections.shuffle(list);
        System.out.println(list.subList(0, 4));
        System.out.println(list.subList(4, 8));
        System.out.println(list.subList(8, 12));
        System.out.println(list.subList(12, 16));
    }
}

2.向新集合中add數據,每add4個數據break一下

public class Waner {
    public static void main(String[] args) {
        String[] str={"鐵皮", "救護車", "漂移", "十字線", "探長", "飛過天", "爵士", "紅蜘蛛", "震盪波", "擎天柱", "大黃蜂", "威震天", "熱破", "補天士", "通天曉", "猛大帥"};
        List list=new ArrayList();
        List list1=new ArrayList();
        for(int i=0;i<str.length;i++){
            list.add(str[i]);
        }
        while(true){
            list1.clear();
            while (true) {
                Random random = new Random();
                int index = random.nextInt(list.size());
                Object s = list.get(index);
                list1.add(s);
                list.remove(index);
                if (list1.size() == 4) {
                    for (Object o : list1) {
                        System.out.print(o + " ");
                    }
                    break;
                }
            }
            System.out.println();
            if (list.size() == 0) {
                break;
            }
        }
    }
}

3.利用隨機數分組

public class Waner {
    public static void main(String[] args) {
        String[] str={"鐵皮", "救護車", "漂移", "十字線", "探長", "飛過天", "爵士", "紅蜘蛛", "震盪波", "擎天柱", "大黃蜂", "威震天", "熱破", "補天士", "通天曉", "猛大帥"};
        List<String> list = new ArrayList<String>();
        for(int i = 0;i < str.length;i++){
            list.add(str[i]);
        }
        Random ran = new Random();
        String b;
        for(int j = 1;j <= 4;j++){
            System.out.println(j+"組");
            for (int k = 0;k < 4;k++){
                b = list.get(ran.nextInt(list.size()));
                System.out.print("   "+b);
                list.remove(b);
            }
            System.out.println("\n");
        }
}
public class Waner {
    public static void main(String[] args) {
        Random ran = new Random();
        String[] str={"鐵皮", "救護車", "漂移", "十字線", "探長", "飛過天", "爵士", "紅蜘蛛", "震盪波", "擎天柱", "大黃蜂", "威震天", "熱破", "補天士", "通天曉", "猛大帥"};
        int count = 15;
        for (int i = 1; i <= 4; i++) {
            System.out.println("第" + i + "組");
            for (int j = 0; j < 4; j++) {
                if (count == 0) {
                    System.out.println(str[count] + "\t");
                    break;
                }
                int random = ran.nextInt(count--);
                System.out.println(str[random] + "\t");
                str[random] = null;
                str = replace(str);
            }
            System.out.println();
        }
    }

    public static String[] replace(String[] str) {
        String[] strings = new String[str.length - 1];
        int j = 0;
        for (int i = 0; i < str.length; i++) {
            if (str[i] == null) {
                continue;
            }
            strings[j++] = str[i];
        }
        return strings;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章