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);
7587 次点击
所在节点    程序员
24 条回复
hcymk2
2014-09-04 09:57:57 +08:00
LZ 你旁边有java语法书么?
buptlee
2014-09-04 10:00:21 +08:00
@hcymk2 有啊,headfirst java。。。。我哪儿错了啊,bro.
buptlee
2014-09-04 10:07:47 +08:00
@hcymk2 是我犯了低级错误了吗?
hcymk2
2014-09-04 10:23:33 +08:00
@buptlee 先不说continue 想先问下Node的Imsi是什么数据类型?
buptlee
2014-09-04 10:24:32 +08:00
@hcymk2
class Node implements Comparable<Node>{

Integer imsi;
Integer dist;

public int compareTo(Node s){
return dist.compareTo(s.getDist());
}

public Node(){
imsi = -1;
dist = Integer.MAX_VALUE;
}

public Node(Integer imsiNo,int distance){
imsi = imsiNo;
dist = distance;
}

public int getDist(){
return dist;
}

public Integer getImsi(){
return imsi;
}

public void setImsi(Integer _imsi){
imsi = _imsi;
}
}
hcymk2
2014-09-04 10:34:03 +08:00
把== 都改成equals()先
davepkxxx
2014-09-04 10:34:50 +08:00
out: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) {
continue out;
}
}
}
buptlee
2014-09-04 10:38:30 +08:00
@hcymk2 我擦,是不是:
int a =3;
Integer b = 3;
boolean c =(a==b);
c会是false?
谢谢哥们儿提示,真是nice。我太蠢了,唔。
buptlee
2014-09-04 10:40:55 +08:00
您说的是continue的go to 用法是吧,不是这个问题啦,continue还是 终止的是最内层循环。是我数据类型弄错了,不过java这点也太tricky了吧。
hcymk2
2014-09-04 10:50:58 +08:00
那个等于,对刚接触java,而有其他语言经验的人来说有时确实是个坑,没办法的事情。
buptlee
2014-09-04 10:54:05 +08:00
嗯,thanks。一不小心就没注意这点,equals()会把两个参数进行类型转换之后再比较是吗?
ioth
2014-09-04 11:22:45 +08:00
java很强大的,不要内存管理,不要指针,但是类型是强类型。
还得要N多包包,果然是开发智能硬件的利器.
hcymk2
2014-09-04 11:32:30 +08:00
@buptlee equals() 在Object 里面是直接return (this == obj);
其他的子类一般都会重写这个方法 ,就看是怎么重写的了。
像Integer 就是
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
buptlee
2014-09-04 11:33:58 +08:00
@hcymk2 cool!
buptlee
2014-09-04 11:38:44 +08:00
@ioth 嗯嗯,我还是怪我学的不好,还在用c++的思维在编java
ioth
2014-09-04 11:42:19 +08:00
@buptlee 看看什么初学java的常见10个还是20个错误吧。
ioth
2014-09-04 11:43:32 +08:00
java风格喜欢用函数,不喜欢操作符,所谓封装好。
vainly
2014-09-04 12:09:09 +08:00
@buptlee continue是跳过“本次”循环进行下次循环,break结束当前循环。
davepkxxx
2014-09-04 13:03:10 +08:00
如果想避免写java常犯的错误,建议买一本Effective Java看看。
buptlee
2014-09-04 14:36:33 +08:00
@davepkxxx项目进度太快了,来不及看这种书哇,都是摸索着前进。等项目结束了再看

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/131639

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX