Java:for循环由于方法调用而在所有迭代之前中断
我正面临一个奇怪的问题。在下面的代码片段中,当我调用userRoles.removole(strole)时,第二个for循环在一次迭代后中断。列表中有2个元素。第一个for循环执行两次。但第二个只执行一次。所述方法调用返回布尔值。有谁能帮我一下我的密码有什么问题吗Java:for循环由于方法调用而在所有迭代之前中断,java,for-loop,method-call,Java,For Loop,Method Call,我正面临一个奇怪的问题。在下面的代码片段中,当我调用userRoles.removole(strole)时,第二个for循环在一次迭代后中断。列表中有2个元素。第一个for循环执行两次。但第二个只执行一次。所述方法调用返回布尔值。有谁能帮我一下我的密码有什么问题吗 if(userRoles != null) { List<String> roles = userRoles.getRoles(); Stri
if(userRoles != null)
{
List<String> roles = userRoles.getRoles();
String strUserName = userRoles.getUserName();
for(String strRole: roles)
{
System.out.println("role : " + strRole);
}
//for(String strRole: roles)
for(int count = 0; count < roles.size() ; count++)
{
String strRole = roles.get(count);
System.out.println("role before check: " + strRole);
if(ur.hasRoleForUser(strRole, strUserName))
{
System.out.println("role after check: " + strRole);
userRoles.removeRole(strRole);
}
}
System.out.println("role length: " + userRoles.getRoles().size());
if(userRoles.getRoles().size() > 0)
{
ur.addUserRoles(userRoles);
}
blnSuccess = true;
}
if(userRoles!=null)
{
List roles=userRoles.getRoles();
字符串strUserName=userRoles.getUserName();
for(字符串字符串字符串:角色)
{
System.out.println(“角色:+strRole”);
}
//for(字符串字符串字符串:角色)
对于(int count=0;count0)
{
ur.addUserRoles(userRoles);
}
blnsucess=true;
}
循环中断,因为您删除了正在遍历的列表中的一个元素(删除后,列表的大小为1,因此count在循环中,您应该首先收集循环结束后要删除的元素,因为您删除了正在遍历的列表中的一个元素,所以循环中断(在删除后,列表的大小为1,因此count
在循环中,您应该首先收集循环后要删除的元素您的
for
循环在每次迭代之前计算count
由于在第一次迭代中调用了userRoles.removole(strRole)
,因此下次循环将计算roles.size()
,返回值将为1。由于1不大于1(此时count
的值),循环将停止进一步的迭代
正如@kamehl23所提供的,您应该使用列表迭代器。
另一个建议是而不是,以修改当前正在遍历的元素。将为您节省大量的时间
祝你好运。你的
for
循环在每次迭代之前计算count
由于在第一次迭代中调用了userRoles.removole(strRole)
,因此下次循环将计算roles.size()
,返回值将为1。由于1不大于1(此时count
的值),循环将停止进一步的迭代
正如@kamehl23所提供的,您应该使用列表迭代器。
另一个建议是而不是,以修改当前正在遍历的元素。将为您节省大量的时间
祝你好运。谢谢大家的回答。我没有注意到这一点。。。我认为角色列表是一个新的对象,它没有引用对象列表,每个项目都将从中删除。这是我的更新代码
if(userRoles != null)
{
List<String> roles = userRoles.getRoles();
String strUserName = userRoles.getUserName();
for(int count = roles.size()-1; count >= 0 ; count--)
{
String strRole = roles.get(count);
if(ur.hasRoleForUser(strRole, strUserName))
{
userRoles.removeRole(strRole);
}
}
if(userRoles.getRoles().size() > 0)
{
ur.addUserRoles(userRoles);
}
blnSuccess = true;
}
if(userRoles!=null)
{
List roles=userRoles.getRoles();
字符串strUserName=userRoles.getUserName();
对于(int count=roles.size()-1;count>=0;count--)
{
String strole=roles.get(count);
if(您的hasRoleForUser(strorle,strusterName))
{
userRoles.removole(strRole);
}
}
if(userRoles.getRoles().size()>0)
{
ur.addUserRoles(userRoles);
}
blnsucess=true;
}
谢谢大家的回答。我没有注意到这一点。。。我认为角色列表是一个新的对象,它没有引用对象列表,每个项目都将从中删除。这是我的更新代码
if(userRoles != null)
{
List<String> roles = userRoles.getRoles();
String strUserName = userRoles.getUserName();
for(int count = roles.size()-1; count >= 0 ; count--)
{
String strRole = roles.get(count);
if(ur.hasRoleForUser(strRole, strUserName))
{
userRoles.removeRole(strRole);
}
}
if(userRoles.getRoles().size() > 0)
{
ur.addUserRoles(userRoles);
}
blnSuccess = true;
}
if(userRoles!=null)
{
List roles=userRoles.getRoles();
字符串strUserName=userRoles.getUserName();
对于(int count=roles.size()-1;count>=0;count--)
{
String strole=roles.get(count);
if(您的hasRoleForUser(strorle,strusterName))
{
userRoles.removole(strRole);
}
}
if(userRoles.getRoles().size()>0)
{
ur.addUserRoles(userRoles);
}
blnsucess=true;
}
请说明RemoveOLE(str)方法在做什么。为什么会奇怪?您正在从正在枚举的列表中删除一项。2 - 1 = 1. getRoles
显然返回对同一列表的引用removole
shortens.@MargaretBloom是的,你是对的。。。我没有注意到这一点。。。我认为它是一个新对象,它没有引用对象的列表,从中删除的是项目。谢谢你的回复。请展示RemoveOLE(str)方法在做什么。为什么会很奇怪?您正在从正在枚举的列表中删除一项。2 - 1 = 1. getRoles
显然返回对同一列表的引用removole
shortens.@MargaretBloom是的,你是对的。。。我没有注意到这一点。。。我认为它是一个新对象,它没有引用对象的列表,从中删除的是项目。感谢您的回复。使用迭代器将抛出ConcurrentModificationException
,因为RemoveOLE
不可能调用remove