笔记23 | 复习for/while/do...while的几种循环用法
来源:互联网 发布:seo排名软件免费 编辑:程序博客网 时间:2024/05/02 04:47
一.for循环遍历数组的传统方式
/*
* 循环1:循环int数组方法一
*/
int[] i = {1,2,3,4,5};
for (int j = 0; j < i.length; j++) {
int it = i[j];
Log.i("md", "循环1打印结果:"+it);
}
打印结果:
01-01 00:05:25.133: I/md(1651): 循环1打印结果:1
01-01 00:05:25.133: I/md(1651): 循环1打印结果:2
01-01 00:05:25.133: I/md(1651): 循环1打印结果:3
01-01 00:05:25.133: I/md(1651): 循环1打印结果:4
01-01 00:05:25.133: I/md(1651): 循环1打印结果:5
二.for循环遍历数组的第二种方法
不严格的说,Java的第二种for循环基本是这样的格式:for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体借助这种语法,遍历一个数组的操作就可以采取这样的写法:
/*
* 循环2:循环int数组方法二
*/
for(int it : i){
Log.i("md", "循环2打印结果:"+it);
}
打印结果:
01-01 00:05:25.133: I/md(1651): 循环2打印结果:1
01-01 00:05:25.133: I/md(1651): 循环2打印结果:2
01-01 00:05:25.133: I/md(1651): 循环2打印结果:3
01-01 00:05:25.133: I/md(1651): 循环2打印结果:4
01-01 00:05:25.133: I/md(1651): 循环2打印结果:5
三.for循环遍历Collection对象的传统方式
首先,init先会被执行,而且就这一次。你可以在这里初始化你要的循环变量。这里也可以空白,只要有分号就行。然后,就去判断条件,如果条件为真,就执行for中的代码。如果为假,就跳出for循环,执行循环外下面的语句。在执行完一次循环主体之后,会来到increment语句,increment允许你进行变量的更新(也可以留空)。最后将会再一次判断条件,如果为真,就执行循环,如果为假,循环终止。
/*
* 循环3:循环string数组方法一
*/
String[] s = {"a","b","c","d","e"};
Collection collection = java.util.Arrays.asList(s);
for(Iterator iterator = collection.iterator(); iterator.hasNext();){
Object str = iterator.next();
Log.i("md", "循环3打印结果:"+str);
}
打印结果:
01-01 00:05:25.133: I/md(1651): 循环3打印结果:a
01-01 00:05:25.133: I/md(1651): 循环3打印结果:b
01-01 00:05:25.133: I/md(1651): 循环3打印结果:c
01-01 00:05:25.133: I/md(1651): 循环3打印结果:d
01-01 00:05:25.133: I/md(1651): 循环3打印结果:e
四.for循环遍历Collection对象的第二种方法
第二种遍历Collection的简单方式:for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体借助这种语法,遍历一个数组的操作就可以采取这样的写法:
/*
* 循环4:循环string数组方法二
*/
for(Object ob : collection){
Log.i("md", "循环4打印结果:"+ob);
}
打印结果:
01-01 00:05:25.133: I/md(1651): 循环4打印结果:a
01-01 00:05:25.133: I/md(1651): 循环4打印结果:b
01-01 00:05:25.133: I/md(1651): 循环4打印结果:c
01-01 00:05:25.133: I/md(1651): 循环4打印结果:d
01-01 00:05:25.133: I/md(1651): 循环4打印结果:e
五.while循环
只要所设定的条件为真,C语言中while循环就会一直重复执行while循环中的代码。
/*
* while循环
*/
int iw = 5;
while (iw>0)//判断条件
{
iw --;
Log.i("md", "while循环打印结果:"+iw);
}
打印结果:
01-01 00:05:25.134: I/md(1651): while循环打印结果:4
01-01 00:05:25.134: I/md(1651): while循环打印结果:3
01-01 00:05:25.134: I/md(1651): while循环打印结果:2
01-01 00:05:25.134: I/md(1651): while循环打印结果:1
01-01 00:05:25.134: I/md(1651): while循环打印结果:0
六.do...while循环
do...while的特点是在语句的最后面对条件进行判断,和while不同的是do...while一定会保证循环能执行一次。
/*
* do...while循环
*/
int idw = 5;
do {
idw --;
Log.i("md", "do...while循环打印结果:"+idw);
} while (idw>0);//判断条件
打印结果:
01-01 00:05:25.134: I/md(1651): do...while循环打印结果:4
01-01 00:05:25.134: I/md(1651): do...while循环打印结果:3
01-01 00:05:25.134: I/md(1651): do...while循环打印结果:2
01-01 00:05:25.134: I/md(1651): do...while循环打印结果:1
01-01 00:05:25.134: I/md(1651): do...while循环打印结果:0
链接
>笔记22 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(三)
>笔记21 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(二)
>笔记20 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(一)
>笔记19 | 利用MediaRecorder实现录像
>笔记18 | 利用MediaRecorder简单实现录音
END
- 笔记23 | 复习for/while/do...while的几种循环用法
- While() , do{}while(); , for(){} 循环语句的用法
- while、do...while、for循环的使用
- while、do while、for循环
- 循环:for、while、do-while
- while循环、do..while 循环、for循环
- for循环,do....while循环,while循环
- for循环,while循环,do while循环
- if switch 具体用法 循环 while do while for
- 171024-对do-while,while,for语句的复习
- C#中的三种循环:while、do..while..、for循环
- if/switch/while/do while/for/等循环及判断的简要用法案例
- java中循环命令while,for,do while的区别
- 循环语句For While do-while的小练习
- 对while、do while、for循环的理解
- For , while , do…while的死循环.
- while语句,do-while与for循环的介绍
- JAVA004--3种循环对比,for;while;do{}while
- HDU 6182A Math Problem
- LinkedIn高级工程经理、AI负责人龙波:深度学习和宽度学习,我的学习探索之路
- 标准回归:预测数值型数据
- 第八周项目3--对称矩阵压缩存储的实现与应用
- 英伟达GTC 中国站揭幕,深度学习学院(DLI)火热开讲
- 笔记23 | 复习for/while/do...while的几种循环用法
- China: AI’s Future Leader?
- 英伟达GTC中国站开幕:宣布TensorRT3、自动机器处理器Xavier
- Facebook在人工智能的道路上走了多远?
- 谷歌CEO Pichai:希望AI从根本上改变每一台设备的本质
- 不到10美元、比M&M豆还小:它让谷歌首款AI相机Clips梦想成真
- gradle使用
- 剑指offer 日常刷题记录
- 呈现树与DOM树的关系