V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
buptlee
V2EX  ›  程序员

java while 里嵌套 for 循环,在 for 里使用 continue 难道是跳出外层 while 的当次循环??

  •  
  •   buptlee ·
    Xiaoxin2009 · 2014-09-04 09:49:44 +08:00 · 7001 次点击
    这是一个创建于 3515 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我的程序是在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);
    24 条回复    2014-09-06 22:45:35 +08:00
    hcymk2
        1
    hcymk2  
       2014-09-04 09:57:57 +08:00   ❤️ 1
    LZ 你旁边有java语法书么?
    buptlee
        2
    buptlee  
    OP
       2014-09-04 10:00:21 +08:00
    @hcymk2 有啊,headfirst java。。。。我哪儿错了啊,bro.
    buptlee
        3
    buptlee  
    OP
       2014-09-04 10:07:47 +08:00
    @hcymk2 是我犯了低级错误了吗?
    hcymk2
        4
    hcymk2  
       2014-09-04 10:23:33 +08:00   ❤️ 1
    @buptlee 先不说continue 想先问下Node的Imsi是什么数据类型?
    buptlee
        5
    buptlee  
    OP
       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
        6
    hcymk2  
       2014-09-04 10:34:03 +08:00   ❤️ 1
    把== 都改成equals()先
    davepkxxx
        7
    davepkxxx  
       2014-09-04 10:34:50 +08:00   ❤️ 1
    out: for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
    if (j == 5) {
    continue out;
    }
    }
    }
    buptlee
        8
    buptlee  
    OP
       2014-09-04 10:38:30 +08:00
    @hcymk2 我擦,是不是:
    int a =3;
    Integer b = 3;
    boolean c =(a==b);
    c会是false?
    谢谢哥们儿提示,真是nice。我太蠢了,唔。
    buptlee
        9
    buptlee  
    OP
       2014-09-04 10:40:55 +08:00
    您说的是continue的go to 用法是吧,不是这个问题啦,continue还是 终止的是最内层循环。是我数据类型弄错了,不过java这点也太tricky了吧。
    hcymk2
        10
    hcymk2  
       2014-09-04 10:50:58 +08:00   ❤️ 1
    那个等于,对刚接触java,而有其他语言经验的人来说有时确实是个坑,没办法的事情。
    buptlee
        11
    buptlee  
    OP
       2014-09-04 10:54:05 +08:00
    嗯,thanks。一不小心就没注意这点,equals()会把两个参数进行类型转换之后再比较是吗?
    ioth
        12
    ioth  
       2014-09-04 11:22:45 +08:00   ❤️ 1
    java很强大的,不要内存管理,不要指针,但是类型是强类型。
    还得要N多包包,果然是开发智能硬件的利器.
    hcymk2
        13
    hcymk2  
       2014-09-04 11:32:30 +08:00   ❤️ 1
    @buptlee equals() 在Object 里面是直接return (this == obj);
    其他的子类一般都会重写这个方法 ,就看是怎么重写的了。
    像Integer 就是
    if (obj instanceof Integer) {
    return value == ((Integer)obj).intValue();
    }
    return false;
    buptlee
        14
    buptlee  
    OP
       2014-09-04 11:33:58 +08:00
    @hcymk2 cool!
    buptlee
        15
    buptlee  
    OP
       2014-09-04 11:38:44 +08:00
    @ioth 嗯嗯,我还是怪我学的不好,还在用c++的思维在编java
    ioth
        16
    ioth  
       2014-09-04 11:42:19 +08:00   ❤️ 1
    @buptlee 看看什么初学java的常见10个还是20个错误吧。
    ioth
        17
    ioth  
       2014-09-04 11:43:32 +08:00   ❤️ 1
    java风格喜欢用函数,不喜欢操作符,所谓封装好。
    vainly
        18
    vainly  
       2014-09-04 12:09:09 +08:00   ❤️ 1
    @buptlee continue是跳过“本次”循环进行下次循环,break结束当前循环。
    davepkxxx
        19
    davepkxxx  
       2014-09-04 13:03:10 +08:00   ❤️ 1
    如果想避免写java常犯的错误,建议买一本Effective Java看看。
    buptlee
        20
    buptlee  
    OP
       2014-09-04 14:36:33 +08:00
    @davepkxxx项目进度太快了,来不及看这种书哇,都是摸索着前进。等项目结束了再看
    ofisheye315
        21
    ofisheye315  
       2014-09-05 08:55:39 +08:00   ❤️ 1
    多层循环中使用break/continue 推荐用标记
    outer:for(int i = 0; i < 10; i++) {
    inner:while(flag){
    //do sth
    continue outer;
    }
    }
    ioth
        22
    ioth  
       2014-09-05 16:15:23 +08:00   ❤️ 1
    @buptlee 磨刀不误。
    buptlee
        23
    buptlee  
    OP
       2014-09-06 22:44:59 +08:00
    @ofisheye315 又学到一招,简直不谈,。
    buptlee
        24
    buptlee  
    OP
       2014-09-06 22:45:35 +08:00
    @ioth 嗯,京东已经同意配送了,嘿嘿
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2370 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:08 · PVG 00:08 · LAX 09:08 · JFK 12:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.