java while 里嵌套 for 循环,在 for 里使用 continue 难道是跳出外层 while 的当次循环??
2014-09-04 09:49:44 +08:00
buptlee
我的程序是在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);