[英]how to find minimum number in for loop?
我想在rolePriority中找到最小值。 我如何在for循环中获得最小编号。
UserRole.java
public class UserRole implements Serializable{
private Integer id;
private User user;
private Role role;
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());
}
}
如果要在任何用户角色中找到最低的角色优先级,则可以尝试以下操作:
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());
}
}
说明:
变量minPriority
初始化为-1
作为占位符值。 在循环的每次迭代,当前的优先级( rolePriority
)抵靠为止(看到的最小值相比minPriority
),并将该最小值可能被更新。 同样,在循环的第一次迭代中,将minPriority
分配给rolePrioirity
。 如果您的优先级可以采用值-1
,则应将minPriority
初始化为某个从未实际使用的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.