以下是一个示例的Java代码实现:

import java.util.List;

class User {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class DataAllocation {
    public static void main(String[] args) {
        int lastAllocationIndex = 3; // 上次分配的序号
        List<User> dataList = List.of(
                new User("User1"),
                new User("User2"),
                new User("User3"),
                new User("User4"),
                new User("User5"),
                new User("User6")
        ); // 本次需要分配的数据List<User>

        int allocationIndex = lastAllocationIndex;
        for (User user : dataList) {
            allocationIndex = allocateData(allocationIndex, user);
        }
    }

    public static int allocateData(int currentAllocationIndex, User user) {
        int nextAllocationIndex = currentAllocationIndex + 1;
        if (nextAllocationIndex > 4) {
            nextAllocationIndex = 1;
        }

        // 分配数据到序号为nextAllocationIndex的位置
        System.out.println("分配数据:" + user.getName() + " 到序号 " + nextAllocationIndex);

        return nextAllocationIndex;
    }
}

以上代码中,lastAllocationIndex表示上次分配的序号,dataList是本次需要分配的数据列表。使用allocateData方法进行分配,方法接受当前的分配序号和要分配的数据作为参数,返回下次分配的序号。

main方法中,我们首先定义了lastAllocationIndex为3,表示上次分配的序号是3。然后,定义了一个包含6个User对象的dataList作为本次需要分配的数据。

接下来,我们使用allocateData方法进行数据分配。在循环中,依次遍历dataList中的每个User对象,通过调用allocateData方法进行分配,并将返回的下次分配序号更新给allocationIndex变量。

allocateData方法中,我们首先计算下次分配的序号nextAllocationIndex,如果超过4则循环回到1。然后,输出分配的数据和序号。

执行以上代码,将会输出类似以下的结果:

分配数据:User1 到序号 4
分配数据:User2 到序号 1
分配数据:User3 到序号 2
分配数据:User4 到序号 3
分配数据:User5 到序号 4
分配数据:User6 到序号 1

以上结果表示按照序号顺序依次分配了数据

java 1入参上次分配至哪个序号本次需要分配的数据ListUser2根据上次分配序号依次1-4按顺序继续分配

原文地址: http://www.cveoy.top/t/topic/h9EA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录