集合的问题,9条数据随机分成3组不能重复

比如:第一组取第1 4 7条数据,第二组取第2 3 6条数据,第三组取第 5 8 9条数据,该怎么做

不一定是9条,但是是可以平均分的

import cn.hutool.core.util.RandomUtil;
RandomUtil.randomEleList(list, 1);
第一个参数是集合,第二个参数是从集合中取几条数据
返回一个同类型集合

取模,比如9条数据分3组,让组数为模数,将数据分为模3余0,模3余1和模3余2的。

生产一个不重复的随机数组,大小为n,然后给n个元素分配数组值为下标存入另外的数组中

不知道怎么说,给你代码看看

// 需要拆分的数组
    private List<Integer> numbers = new ArrayList<>();

    private void split() {
        int numberCount = 9; //数组内的数量
        int splitCount = 3; //拆分组数

        for (int i = 0; i < numberCount; i++) {
            numbers.add(i);
        }

        //拆分结果数组
        List<List<Integer>> splitList = new ArrayList<>();
        for (int i = 0; i < splitCount; i++) {
            splitList.add(new ArrayList<>());
        }

        int maxIndex = numbers.size() - 1;
        int currentSplitIndex = 0;
        Random r = new Random();
        while (maxIndex > 0) {
            int index = r.nextInt(maxIndex);
            splitList.get(currentSplitIndex).add(numbers.get(index));
            numbers.remove(index);
            maxIndex--;
            currentSplitIndex = getNextIndex(currentSplitIndex, splitCount);

            if (maxIndex == 0){
                splitList.get(currentSplitIndex).add(numbers.get(0));
            }
        }
    }

    private int getNextIndex(int currentIndex, int splitCount){
        currentIndex++;
        if (currentIndex == splitCount)
            currentIndex = 0;
        return currentIndex;
    }