[英]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.