如果1个以上实例具有相同(最小)值,如何从确定最小值的循环中随机选择一个对象?

时间:2019-03-12 13:57:14

标签: java arrays random foreach seed

我编写了一个Java程序,该程序包含三个相关的类;员工,客户和合同。在我的主要方法中,我已经创建了一个包含所有3个类的各种实例的数组;

// Create Clients
Client Client1 = new Client(1, "Client 1", 0);
Client Client2 = new Client(2, "Client 2", 0);

// Create Array of Clients
Client[] clients = new Client[] {Client1, Client2};

// Create Contracts
Contract Contract1 = new Contract("Contract 1", 1, 850, 4, 0, 1, 0);
Contract Contract2 = new Contract("Contract 2", 2, 500, 4, 0, 1, 0);
Contract Contract3 = new Contract("Contract 3", 3, 1500, 3, 0, 1, 0);

// Create Array of Contracts
Contract[] contracts = new Contract[] {Contract1, Contract2, Contract3};

// Create Employees
Employee Employee1 = new Employee(1, "Bradley", 0);
Employee Employee2 = new Employee(2, "Patrick", 0);
Employee Employee3 = new Employee(3, "Erin", 0);
Employee Employee4 = new Employee(4, "Jim", 0);
Employee Employee5 = new Employee(5, "Fredrick", 0);

// Create Array of Employees
Employee[] employees = new Employee[] {Employee1, Employee2, Employee3, Employee4, Employee5};

我在Contract.Java中有一个函数,名为AssignContractToEmployeeWithLeastContracts,它带有2个参数,一组雇员和合同。功能是:

// Assign contract to employee with least contracts
public void assignContractToEmployeeWithLeastContracts(Employee[] employees, Contract contract) {

    // Assign to employee with minimum contracts
    int minContract = Integer.MAX_VALUE;
    Employee employeeWithMinContracts = null;
    for (Employee employee : employees) {
        if (employee.getCurrentlyAssignedContracts() < minContract) {
            // swap min and employee if true
            employeeWithMinContracts = employee;
            minContract = employeeWithMinContracts.getCurrentlyAssignedContracts();
        }
    }

    employeeWithMinContracts.assignContract(employeeWithMinContracts, contract);
}

我的问题是,如果2名员工都分配了相同(最少)张合同,那么我该如何处理。如果发生这种情况,我该如何随机选择一名员工?我曾尝试在foreach循环中实现种子,但不断破坏一切。

任何帮助都将得到帮助,

谢谢, B

2 个答案:

答案 0 :(得分:3)

如果您希望两名以上的员工共享相同数量的最低合同,那么您需要从中随机挑选。您可以通过列出最低限度合同并随机选择一个合同来完成此操作。

ORA-00936: missing expression

答案 1 :(得分:2)

如果我的理解正确,那么您想使用assignContractToEmployeeWithLeastContracts方法将合同随机分配给其中一名雇员,如果他们拥有相同数量的合同。
因为当前您将其分配给合同数量最少的第一位员工。

您的方法将变为:

public void assignContractToEmployeeWithLeastContracts(Employee[] employees, Contract contract) {
    int minContract = Integer.MAX_VALUE;
    List<Employee> employeesWithMinContracts = new ArrayList<Employee>();
    for (Employee employee : employees) {
        if (employee.getCurrentlyAssignedContracts() < minContract) {
            employeesWithMinContracts.clear();
            employeesWithMinContracts.add(employee);
            minContract = employee.getCurrentlyAssignedContracts();
        } else if (employee.getCurrentlyAssignedContracts() == minContract) {
            employeesWithMinContracts.add(employee);
        }
    }
    int randomIndex = (int)(Math.random() * employeesWithMinContracts.size());
    employeesWithMinContracts.get(randomIndex).assignContract(contract);
}

我们将利用Math.random返回一个介于[0,1]之间的值的事实,从而可以随机找到我们的employeeWithMinContracts列表索引。

备注:

  • 注意,我删除了Employee方法中的assignContract作为参数。由于此方法已在相关的Employee上调用。

  • 此外,您的实例名称应以小写字母开头,以符合Java标准:
    Contract1成为contract1
    Employee1成为employee1
    等等...