简体   繁体   English

如何在for循环中找到最小数量?

[英]how to find minimum number in for loop?

I want to find min number in rolePriority. 我想在rolePriority中找到最小值。 How can i get min number in the for loop. 我如何在for循环中获得最小编号。

UserRole.java UserRole.java

public class UserRole implements Serializable{
    private Integer id;
    private User user;
    private Role role;

RoleAction.java RoleAction.java

 public class RoleAction implements Serializable{ private Integer id; private Role role; private Action action; } 
for (UserRole userRole : userRoles) {
            dbRoles.add(userRole.getRole().getRoleName());

            int rolePriority=userRole.getRole().getPriority();



            for (RoleAction ra: userRole.getRole().getRoleActions()) {

                System.out.println("#########"+ra.getAction().getName());
                System.out.println("@@@@@@@@@"+ra.getAction().getId());

                map.put(ra.getAction().getName(), ra.getAction().getId());

            }

        }

If you want to find the minimum role priority among any user role, then you can try the following: 如果要在任何用户角色中找到最低的角色优先级,则可以尝试以下操作:

int minPriority = -1;
for (UserRole userRole : userRoles) {
    dbRoles.add(userRole.getRole().getRoleName());

    int rolePriority = userRole.getRole().getPriority();
    if (rolePriority < minPriority || minPriority == -1) {
        minPriority = rolePriority;
    }
    for (RoleAction ra: userRole.getRole().getRoleActions()) {
        System.out.println("#########"+ra.getAction().getName());
        System.out.println("@@@@@@@@@"+ra.getAction().getId());
        map.put(ra.getAction().getName(), ra.getAction().getId());
    }
}

Explanation: 说明:

The variable minPriority is initialized to -1 as a placeholder value. 变量minPriority初始化为-1作为占位符值。 During each iteration of the loop, the current priority ( rolePriority ) is compared against the smallest value seen so far ( minPriority ), and this minimum value is possibly updated. 在循环的每次迭代,当前的优先级( rolePriority )抵靠为止(看到的最小值相比minPriority ),并将该最小值可能被更新。 Also, during the first iteration of the loop, minPriority is assigned to the rolePrioirity . 同样,在循环的第一次迭代中,将minPriority分配给rolePrioirity If your priorities can take the value -1 then you should initialize minPriority to some value which is never actually used. 如果您的优先级可以采用值-1 ,则应将minPriority初始化为某个从未实际使用的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM