Java循环中标签的作用(转)
转自:http://lihengzkj.iteye.com/blog/1090034
以前不知道在循环中可以使用标签。最近遇到后,举得还是有其独特的用处的。我这么说的意思是说标签在循环中可以改变循环执行的流程。而这种改变不是我们以前单独使用break或者是continue能够达到的。下面还是看看实例吧。
- outer1:
- for(int i =0;i<4;i++){
- System.out.println("begin to itrate. "+i);
- for(int j =0;j<2;j++){
- if(i==2){
- continue outer1;
- // break;
- }
- System.out.println("now the value of j is:"+j);
- }
- System.out.println("******************");
- }
执行的结果是:
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后面不会出现一串*号了。
对比:
- outer1:
- for(int i =0;i<4;i++){
- System.out.println("begin to itrate. "+i);
- for(int j =0;j<2;j++){
- if(i==2){
- // continue outer1;
- break;
- }
- System.out.println("now the value of j is:"+j);
- }
- System.out.println("******************");
- }
注:我们直接使用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+标签的效果
- outer2:
- for(int i =0;i<4;i++){
- System.out.println("begin to itrate. "+i);
- for(int j =0;j<2;j++){
- if(i==2){
- break outer2;
- // break;
- }
- System.out.println("now the value of j is:"+j);
- } System.out.println("******************");
- }
结果:
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中的标签只适合与嵌套循环中使用。
Java循环中标签的作用(转)的更多相关文章
- Java 循环中标签的作用
continue和break可以改变循环的执行流程,但在多重循环中,这两条语句无法直接从内层循环跳转到外层循环.在C语言中,可以通过goto语句实现多重循环的跳转,但在非循环结构中使用goto语句会使 ...
- Java循环中删除一个列表元素
本文主要想讲述一下我对之前看到一篇文章的说法.假设跟你的想法有出入,欢迎留言.一起讨论. #3. 在循环中删除一个列表元素 考虑以下的代码.迭代过程中删除元素: ArrayList<String ...
- return break 和continue在for循环中的不同作用
平时自己经常在函数里见到return,在switch语句中使用break,而continue则用的不多. 其实这三者都能在for循环中发挥不同的作用,让代码更加灵活. 先说return return是 ...
- 解决 java循环中使用 Map时 在put值时value值被覆盖的问题
其实很简单,只需要把容器换成list 然后在循环中,每次循环末尾map = new HashMap() 或者直接在循环中一开始就实例化hashmap(Map map = new HashMap();) ...
- Java中for循环以及循环中标签
1.第一种,通过迭代的方式 File[] listFiles = file.listFiles(); for (Iterator iterator = files.iterator(); iterat ...
- 循环中标签outer用法:break outer continue outer
1.outer: break 如果不使用标签,break跳出里层for循环,使用break标签,则跳出两层循环 输出:i:0 j:0 i:0 j:1 public class breakTest { ...
- Java程序中的代理作用和应用场景及实现
body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...
- break 和continue在循环中起到的作用
break语句的作用是终止当前循环,跳出循环体.主意,break只能跳出一层循环. continue语句的作用是终止本轮循环并开始下一轮循环,(这里要主意的是在开始下一轮循环之前,会先测试循环条件). ...
- java 构造方法中super()的作用?
手贱百度了一下 :java里面自定义类的有参构造方法为什么不用super() 举个例子: class Father { Father(){print ('father');}; } class Son ...
随机推荐
- php定时回调接口
http://www.dewen.org/search/q/php%E5%AE%9A%E6%97%B6%E8%B0%83%E7%94%A8%E5%AD%98%E5%82%A8%E8%BF%87%E7% ...
- Java工程Properties配置文件注释中文,会自动转换为其他编码方式问题解决 中文乱码
properties文件中想注释中文,但是写出来后却是 :# /4djf/234/4354/r23df/324d 这种效果 是因为字符编码默认没有设置造成的,以前总是安装插件解决此问题, 但是却牺牲 ...
- spring mvc 接受前台json @RequestBody json 属性 空 使用 JsonProperty spring mvc 获取json转乘bean
请给json序列序列化成的javabean 属性加上 @JsonProperty(value = "real_name") 否则 springmvc 可能接受不到数据 ja ...
- IE11 F12工具报错
系统环境 win7+IE11 报错描述: Exception in window.onload: Error: An error has ocurredJSPlugin.3005 Stack Trac ...
- 25个可遇不可求的jQuery插件
随着jQuery插件在网站建设过程中的使用率不断的增加,所以有必要跟进时代步伐开发出一些新的插件/代码片段,以此来巩固并提高前端用户体验,将用户体验提升到一个新的高度. 接下来所推荐的这些插件中有滑块 ...
- 条件随机场 (CRF) 分词序列谈之一(转)
http://langiner.blog.51cto.com/1989264/379166 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.ht ...
- Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式
背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前 ...
- GitHub最新命令使用教程
一.创建github仓库并提交代码 1.在github创建public仓库 2.会生成一个git地址 https://github.com/ae6623/Zebra.git 3.在本地打开命令,在文件 ...
- robot framework + python实现http接口自动化测试框架
https://www.jianshu.com/p/6d1e8cb90e7d 前言 下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测 ...
- maven 下载源码downloadsources
mvn eclipse:eclipse -Ddownloadsources=true -Ddownloadjavadocs=true