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 ...
随机推荐
- [转]Mysql中的SQL优化与执行计划
From : http://religiose.iteye.com/blog/1685537 一,如何判断SQL的执行效率? 通过explain 关键字分析效率低的SQL执行计划. 比如: expla ...
- HDOJ 4869 Turn the pokers
最后的结果中正面向上的奇偶性是一定的,计算出正面向上的范围low,up 结果即为 C(m.low)+ C(m.low+2) +.... + C(m,up) ,用逆元取模 Turn the pokers ...
- iOS:移动端“用户反馈和客服”的几个平台SDK的介绍
简单阐述: 用户反馈功能几乎是每个app都有的一个功能点,通过反馈功能实现与用户的连接.沟通,随时随地收集用户意见反馈和Bug报告,即时和用户保持沟通,在一定程度上提升了app的竞争力.而给app评分 ...
- poj 4468Spy(kmp算法)
Spy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 3660 Alice and Bob's Trip(树形DP)
Alice and Bob's Trip Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- iOS截屏方法
//获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...
- 如何在CentOS 7.2上创建NFS的Share,然后让Client可以访问
讲得详细清楚明白的好文. Setting Up an NFS Server and Client on CentOS 7.2 https://www.howtoforge.com/tutorial/s ...
- iOS开发-UIScreenEdgePanGestureRecognizer实战
UIScreenEdgePanGestureRecognizer名字很长,而且关于其文档也是少的的可怜,苹果官方给的唯一的一个属性是edges,文档中的解释是这样的: A UIScreenEdgePa ...
- scikit-learn——快速入门 - daniel-D(转)
ML sklearn快速入门 申明:该系列博客是学习 sklearn 的笔记,内容将涵盖大部分机器学习的方法.本人微博@迅猛龙Daniel,能力有限,存在任何问题,希望共同交流.该博客采用马克飞象专业 ...
- KDiff
BeyondCompare是收费的,用了一段时间不能用了.找到一个 KDiff做对比工具,也很好用. 在这里下载: http://sourceforge.net/projects/kdiff3/fil ...