• java中循环标签的使用


     以前不知道在循环中可以使用标签。最近遇到后,举得还是有其独特的用处的。我这么说的意思是说标签在循环中可以改变循环执行的流程。而这种改变不是我们以前单独使用break或者是continue能够达到的。下面还是看看实例吧。 
       

    Java代码  收藏代码
    1. outer1:  
    2. for(int i =0;i<4;i++){  
    3.     System.out.println("begin to itrate.    "+i);  
    4.     for(int j =0;j<2;j++){  
    5.         if(i==2){  
    6.             continue outer1;  
    7. //          break;  
    8.         }  
    9.         System.out.println("now the value of j is:"+j);  
    10.     }  
    11.     System.out.println("******************");  
    12. }  
    13.       


    执行的结果是: 
    begin to itrate.    0 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    begin to itrate.    1 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    begin to itrate.    2 
    begin to itrate.    3 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    注:当i=2的时候,continue outer1 使程序回到了outer1最开始循环的位置,开始下一次循环,这个时候执行的循环是i=3而不是重新从i=0开始。同时当使用continue outer1跳出内层循环的时候,外层循环后面的语句也不会执行。也就是是在begin to itrate.    2后面不会出现一串*号了。 
    对比: 

    Java代码  收藏代码
    1. outer1:  
    2. for(int i =0;i<4;i++){  
    3.     System.out.println("begin to itrate.    "+i);  
    4.     for(int j =0;j<2;j++){  
    5.         if(i==2){  
    6. //          continue outer1;  
    7.             break;  
    8.         }  
    9.         System.out.println("now the value of j is:"+j);  
    10.     }  
    11.     System.out.println("******************");  
    12. }  


    注:我们直接使用break的话,只是直接跳出内层循环。结果其实就可以看出区别来: 
    begin to itrate.    0 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    begin to itrate.    1 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    begin to itrate.    2 
    ****************** 
    begin to itrate.    3 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    -----------------------------------------------------------------分割线 
    我们再来看看break+标签的效果 

    Java代码  收藏代码
    1. outer2:  
    2. for(int i =0;i<4;i++){  
    3.     System.out.println("begin to itrate.    "+i);  
    4.     for(int j =0;j<2;j++){  
    5.         if(i==2){  
    6.             break outer2;  
    7. //          break;  
    8.         }  
    9.         System.out.println("now the value of j is:"+j);  
    10.     }           System.out.println("******************");  
    11. }  


    结果: 
    begin to itrate.    0 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    begin to itrate.    1 
    now the value of j is:0 
    now the value of j is:1 
    ****************** 
    begin to itrate.    2 
    注:从结果就可以看出当i=2的时候,break+标签 直接把内外层循环一起停掉了。而如果我们单独使用break的话就起不了这种效果,那样只是跳出内层循环而已。 
    最后说一句,Java中的标签只适合与嵌套循环中使用。

  • 相关阅读:
    windows xp查看缩略图时有缩略图没有文件名
    数据库的相关操作
    使用timer控件创建一个简单的报警程序
    xp_sendmail的正确配置与使用
    SQL Server 索引结构及其使用(三)
    启动与关闭服务器
    不间断连续图片滚动效果的制作方法
    使用C#调用外部Ping命令获取网络连接情况
    SQL Server 索引结构及其使用(一)
    winform 与asp.net 下拉列表的区别
  • 原文地址:https://www.cnblogs.com/lichengze/p/5720522.html
Copyright © 2020-2023  润新知