BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #34359同步于 2014/9/4
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

java while 里嵌套 for 循环,在 for 里使用 continue 难道是跳

binxin
2014/9/4镜像同步9 回复
程序逻辑非常简单,大部分代码都是为了打log。 我的程序是在while循环里面有个for循环,dist_matrix是一个 HashMap<Integer,ArrayList<Integer>> clusterSet = new HashMap<Integer,ArrayList<Integer>>(3200000); 程序的目的是要对每一个key-value对进行更新,代码在下面,逻辑很简单,就是对每个键的值(一个list)进行更新: 1,如果不包含user1和user2,list不变. 2,如果包含user1或者user2中的一个,把这个节点改成user1. 3, 如果既包含user1又包含user2,则保留第一次遇到的节点,并把标识换成user1,删除第二次遇到的节点。 说说我遇到的奇怪的问题。有一个key-value对是这样的: the user1 and user2 are : 2,105529 the value of 116615 is : 2-0,105529-0,1132145-0,1946439-0,2155282-0,2854-1,46590-1,449056-1,491556-1,693681-1, the first time occurs in the key : 116615 the first time occurs in the key : 116615, the item is user1 : 2 start to check the value of : 116615 the modified value of 116615 is : 2-0,105529-0,1132145-0,1946439-0,2155282-0,2854-1,46590-1,449056-1,491556-1,693681-1, 按逻辑来看修改后的116615的值应该是没有第二项105529-0的,应为这是第二次遇到(user2),应该是被删除了,至少应该打这条log吧: System.out.println("the second time occurs in the key : "+key+", the item is : "+node.getImsi()); 但是没有,说明这个地方的逻辑都没执行进来,查看代码,实在找不出问题,我想问问各位对java的逻辑控制熟悉的同学,是我的逻辑写错了,还是问题出在continue那里?continue直接就跳出整个for循环,去执行下次while循环了,虽然听起来有点不可思议,但是我实在想不出别的解释啊,谢谢各位了,帮小弟看看。 boolean IsExist = false; Iterator<Integer> iter = dist_matrix.keySet().iterator(); while (iter.hasNext()) { IsExist=false; Integer key = iter.next(); LinkedList<Node> value = dist_matrix.get(key); String check_str = ""; for(Node node : value){ check_str=check_str+node.getImsi()+"-"+node.getDist()+","; } System.out.println("the user1 and user2 are : "+user1+","+user2); System.out.println("the value of "+key+" is : "+check_str); for(int i = 0;i<value.size();i++){ Node node = value.get(i); if((user1==node.getImsi() || user2==node.getImsi())&&(IsExist)){ System.out.println("the second time occurs in the key : "+key+", the item is : "+node.getImsi()); value.remove(node); } if((user1==node.getImsi() || user2==node.getImsi())&&(!IsExist)){ System.out.println("the first time occurs in the key : "+key); if(user1==node.getImsi()){ IsExist = true; System.out.println("the first time occurs in the key : "+key+", the item is user1 : "+user1); continue; } else{ System.out.println("the first time occurs in the key : "+key+", the item is user2 : "+user2); node.setImsi(user1); IsExist = true; continue; } } } System.out.println("start to check the value of : "+check); String modified_check_str = ""; LinkedList<Node> modified_value = dist_matrix.get(check); for(Node node : modified_value){ modified_check_str=modified_check_str+node.getImsi()+"-"+node.getDist()+","; } System.out.println("the modified value of "+check+" is : "+modified_check_str);
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
taoch机器人#1 · 2014/9/4
。。continue没研究过,break大概了解一些:break是只跳出当前层次的循环。要一次跳出多重循环,可以使用label指定跳到哪里。不过不建议这么做,因为break + label有点像goto那种程序执行流程了
binxin机器人#2 · 2014/9/4
【 在 taoch 的大作中提到: 】 : 。。continue没研究过,break大概了解一些:break是只跳出当前层次的循环。要一次跳出多重循环,可以使用label指定跳到哪里。不过不建议这么做,因为break + label有点像goto那种程序执行流程了 嗯,continue也可以的: out: for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) { continue out; } } } 刚刚解决了程序的逻辑问题,原因是: int a =3; Integer b = 3; boolean c =(a==b); c会是false[ema16]
binxin机器人#3 · 2014/9/4
【 在 taoch 的大作中提到: 】 : 。。continue没研究过,break大概了解一些:break是只跳出当前层次的循环。要一次跳出多重循环,可以使用label指定跳到哪里。不过不建议这么做,因为break + label有点像goto那种程序执行流程了 谢谢桃吉哥解答[ema15]
cowfighting机器人#4 · 2014/9/4
我运行是true呀...求解释 【 在 binxin 的大作中提到: 】 : 嗯,continue也可以的: : out: for (int i = 0; i < 10; i++) { : for (int j = 0; j < 10; j++) { : ...................
lixing机器人#5 · 2014/9/4
能写成代码的形式不?你这样让人看你的逻辑很头疼的
binxin机器人#6 · 2014/9/4
【 在 lixing 的大作中提到: 】 : 能写成代码的形式不?你这样让人看你的逻辑很头疼的 sorry啦,贴进来的时候自动变成这种版面了。 不过问题解决了。thanks
msmvp机器人#7 · 2014/9/4
不管是continue还是break,都是只跳出包含continue和break的最里面的可见的那层循环。 for(){for{xxoo;continue;fuck;}}fuck就不会得到执行了
binxin机器人#8 · 2014/9/4
【 在 msmvp 的大作中提到: 】 : 不管是continue还是break,都是只跳出包含continue和break的最里面的可见的那层循环。 : for(){for{xxoo;continue;fuck;}}fuck就不会得到执行了 嗯嗯,确实是这样,不过fuck也是有点xxoo[ema13]
ztrh10211377机器人#9 · 2014/9/4
break 与continue 都是跳出循环体,break是跳出循环体,执行循环体外下一句,continue是跳出本次循环,继续判断下一循环是否满足条件,都是最里面那层。