先看下以下方法的打印结果以及返回值:
public static void main(String[] args) { System.out.println("返回值:" + testResult()); } public static boolean testResult() { for(int i=1; i<=5; i++) { System.out.println("-------------->开始:" + i); if(i == 3) { return true; } System.out.println("-------------->结束:" + i); } return true; }
打印结果:
-------------->开始:1
-------------->结束:1
-------------->开始:2
-------------->结束:2
-------------->开始:3
返回值:true,说明在for里return一个值的话相当于退出循环。
1)假设我们对testResult方法进行重构,抽离出for里面的逻辑到一个单独的方法:
public static boolean testResult() { for(int i=1; i<=5; i++) { test1(i); } return true; } public static void test1(int i) throws NullPointerException{ System.out.println("-------------->开始:" + i); if(i == 3) { return; } System.out.println("-------------->结束:" + i); }
同样放在main方法中。只不过testResult方法的for循环里直接调的重构的方法,打印结果:
-------------->开始:1
-------------->结束:1
-------------->开始:2
-------------->结束:2
-------------->开始:3
-------------->开始:4
-------------->结束:4
-------------->开始:5
-------------->结束:5
返回值:true
这说明,test1(i)方法用return;语句试图走到i=3的时候中断; 但是循环还是走完了。
2)不妨给for循环里调用的方法一个返回值,如下:
public static boolean testResult() { for(int i=1; i<=5; i++) { return test2(i); } return true; } public static boolean test2(int i) throws NullPointerException{ System.out.println("-------------->开始:" + i); if(i == 3) { return true; } System.out.println("-------------->结束:" + i); return false; }
打印结果如下:
-------------->开始:1
-------------->结束:1
返回值:false
这说明,在for里调用一个有boolean返回值的方法,会让方法还没走到i=3就断掉,返回一个boolean值。
3)在for循环里需要根据条件return一个boolean值时。for循环里面的代码若需要重构成一个方法时,应该是有返回值的,但这个返回值不能是boolean,我们不妨用String代替,而在for循环里面用返回的String标记来判断是否退出循环~~
改造如下:
public static boolean testResult() { for(int i=1; i<=5; i++) { String flag = test3(i); if("yes".equals(flag)) { return true; } } return true; } public static String test3(int i) throws NullPointerException{ System.out.println("-------------->开始:" + i); if(i == 3) { return "yes"; } System.out.println("-------------->结束:" + i); return "no"; }
打印结果:
-------------->开始:1
-------------->结束:1
-------------->开始:2
-------------->结束:2
-------------->开始:3
返回值:true
说明达到了最初未对for循环里面的代码进行重构时的效果~
以上的小例子是我在对类似代码进行重构时报错的经验小结,因为实际代码里,for里面的代码重复了好几次,但是又因为for里面的代码需要根据判断条件return一个boolean值。在重构的过程中,我先是改成test1(i),再改成test2(i), 最后改成test3(i)才该对,达到未重构时的效果。
希望各位同仁们遇到for循环里面需要return;或return true/false;时,一定要谨慎哈~
相关推荐
java跳出循环break;return;continue使用 . java跳出循环break;return;continue使用 . java跳出循环break;return;continue使用
在网上看了一些for循环的效率问题,发现基本上都是一些理论的东西,且大多都是Copy来的,前后文自相矛盾。自己做了一个例子,来看看他们的效率 1.最慢 private long getTime1() { long time = 0; int count = 0;...
return [lambda x: x**i for i in range(1,5,2)] print([f(3) for f in foo()]) 伙伴们,你们认为这里产生的结果是什么呢?我们再来看下这题的变体: 代码:2 def foo(): functions=[] for i in range(1,5,2): ...
主要介绍了break、continue、return在for循环中的使用,本文是小编收藏整理的,非常具有参考借鉴价值,需要的朋友可以参考下
如下所示: a = [1, 2, 3, 8, 9] ...以上这篇在Python中,不用while和for循环遍历列表的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文章:pytho
这适用于单条数据的处理场景,如果是在for循环中处理多条数据,依次处理每条数据的异步操作呢? 思路:还是利用Promise的then()来进行链式操作,每次循环的时候往Promise后添加一个新的then(),这样就可以保证上一个...
Optimal pricing and return policies for perishable commodities Optimal pricing and return policies for perishable commodities
一开始接触到vue的for循环就觉得贼好用,重复性的东西再也不用ctrl C+ctrl V了好吗。不仅代码可复用了,而且提高了代码可读性、减轻了工作量,更重要的是写代码都有成就感了。 贴出个小小例子: template <ul v-for...
这篇文章主要介绍了Python for循环与getitem的关系详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一个类里面如果由__iter__for循环就是找它取,没有的话...
JS中for,for...in,for...of和forEach的区别和用法实例 ...for循环中可以使用return、break等来中断循环。 //遍历数组 var arr = [1,2,3,4,5]; for(var i=0;i;i++){ console.log(arr[i]); } //遍历对
这篇文章主要介绍了JS 使用for循环配合数组遍历子节点查找元素 function nextChildNode(node,clazz,tagName){ var count= node.childElementCount; for(var i=0;i<count;i++){ if(node==undefined || node....
return oriented programming for the arm architecture
本文主要讲了单片机return的用法,下面一起来学习一下
return i < n1 def cond2(i, a, b): return i < n2 def cond3(i, a, b): return i < n3 def body(i, a, b): return i + 1, b, a + b i1, a1, b1 = tf.while_loop(cond1, body, (2, 1,
#include int main() { int n; scanf("%d",&n); int i,j,xuanze,t; int a[256]; for(i=0;i;i++) scanf("%d",&a[i]); for(i=0;i;i++) { xuanze=i; for(j=i+1;... for(i=0;... return 0; }
C# return 返回的几种用法 return View(); 返回和控制器同名视图 return Content("我是一段文本", "text/html"); return Redirect("http://www.baidu.com/"); 返回URL重定向 还有好多种用法哦,下载了看吧。
for循环的简化语法。 安装 npm install for-range 用法 const forRange = require ( "for-range" ) ; forRange ( { min : 2 , max : 10 , step : 2 } , ( i ) => { if ( i === 4 ) return ; // Skip for 4 if ( i...
工程结构 views.py def home(request): TutorialList = [HTML, ... return render(request, 'home.html', {'TutorialList': TutorialList}) url.py ...基本的 for 循环 和 List内容的显示</title> </head>
查些资料,并且试验过发现下面方法可以 1、for循环中我们使用continue;终止本次循环计入下一个循环,使用break终止整个循环。 2、而在jquery中 $.each则对应的使用return true 和return false。 break可以跳出switch...
我们从一个测试开始。下面这个函数的功能是什么? def foo(lst): a = 0 for i in lst: a += i ... for t in lst: ... return a, b ...如果你觉得它的功能是“计算lst中所有元素的和与...事实上,Python正式声明过,为fo