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 ...
随机推荐
- Zookeeper Tutorial 1 -- Overview
ZooKepper: 一个分布式应用的分布式协调服务(Distributed Coordination Service) 分布式服务难以管理, 他们容易造成死锁和竞争, ZooKepper的动机就是为 ...
- Guava Finalizer
/* * Copyright (C) 2008 The Guava Authors Licensed under the Apache License, Version 2.0 (the " ...
- @Autowired注解和静态方法 NoClassDefFoundError could not initialize class 静态类
NoClassDefFoundError could not initialize class 静态类 spring boot 静态类 java.lang.ExceptionInInitializer ...
- Python 的 Flask 框架安装应用
Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱採用 Werkzeug ,模板引擎则使用 Jinja2 ,使用 BSD 授权. Flask也被称为 " ...
- mysql 触发器 插入
MySQL触发器 重要学习资料: http://dev.mysql.com/doc/refman/5.1/zh/triggers.html 在使用触发器的时候,遇到一个错误: [SQL]insert ...
- logistic回归算法及其matlib实现
一般来说,回归不用在分类问题上,因为回归是连续型模型,而且受噪声影响比较大.如果非要使用回归算法,可以使用logistic回归. logistic回归本质上是线性回归,只是在特征到结果的映射中多加入了 ...
- go语言之进阶篇方法的重写
1.方法的重写 示例: //Person类型,实现了一个方法 func (tmp *Person) PrintInfo() { fmt.Printf("name=%s, sex=%c, ag ...
- 【MySQL】PostgresSQL-MySQL对比
PostgresSQL-MySQL对比 (5 条消息)PostgreSQL 与 MySQL 相比,优势何在? - 知乎 IOC匹配 - 天眼公共空间 - 360企业安全Confluence 调查分析 ...
- nodejs 项目的session验证
原文:https://www.codexpedia.com/node-js/a-very-basic-session-auth-in-node-js-with-express-js/ -------- ...
- [Functional Programming] Using JS, FP approach with Arrow or State Monad
Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...