java list 的 四种遍历方式
在java中遍历一个list对象的方法主要有以下四种:
1. For Loop —— 普通for循环
2. Advanced For Loop —— 高级for循环
3. Iterator Loop —— 迭代器遍历
4. While Loop —— while循环
具体可以参考以下代码:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main { public static void main(String []args) { // create list
List<Integer> lst = new ArrayList<>(); // add some elements
lst.add(1);
lst.add(2);
lst.add(3);
lst.add(4); // 1. for loop
System.out.println("1. For loop");
for (int i = 0; i < lst.size(); ++ i) {
System.out.println(lst.get(i));
} // 2. advanced for loop
System.out.println("2. Advanced For loop");
for (int val : lst) {
System.out.println(val);
} // 3. iterator loop
System.out.println("3. Iterator Loop");
Iterator<Integer> it = lst.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} // 4. while loop
System.out.println("4. While Loop");
int i = 0;
while (i < lst.size()) {
System.out.println(lst.get(i));
++ i;
}
}
}
参考:http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
java list 的 四种遍历方式的更多相关文章
- java Map的四种遍历方式
1.这是最常见的并且在大多数情况下也是最可取的遍历方式,在键值都需要时使用. Map<Integer, Integer> map = new HashMap<Integer, Int ...
- java集合四种遍历方式
package conection; import java.util.Iterator;import java.util.LinkedList;import java.util.List; publ ...
- list的四种遍历方式
1.手先增强for循环和iterator遍历的效果是一样的,也就说 增强for循环的内部也就是调用iteratoer实现的,但是增强for循环 有些缺点,例如不能在增强循环里动态的删除集合内容.不能获 ...
- Map 的四种遍历方式
Map 的四种遍历方式 import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class ...
- lua中for循环的四种遍历方式
lua中for的四种遍历方式区别 table.maxn 取最大的整数key #table 从1开始的顺序整数最大值,如1,2,3,6 #table == 3 key,value pairs 取每一 ...
- java map的四种遍历
四种遍历: public static void main(String[] args) { Map<String, String> map = new HashMap<String ...
- Java HashMap 四种遍历方式
HashMap遍历方式包含以下4种: 1.遍历KeySet,再通过Key来getValue. 2.使用entrySet的迭代器. 3.foreach entrySet的方式. 3.foreache v ...
- map的四种遍历方式
map是Java中非常常用的一种数据结构,但map不同于set和list都继承自Collection接口. 所以map没有实现Collection的Iterator 方法,自身没有迭代器来遍历元素. ...
- Map集合的四种遍历方式
很久以前写的代码,和上一个做比较吧!便于以后查看 import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
随机推荐
- C语言学习——C程序的运行机理
预处理: #include<xxx> 尖括号表示库文件:#include"xxx" 双引号表示自己写的文件. #include后面的文件格式允许多种,但若要是" ...
- http://blog.csdn.net/zhanglvmeng/article/details/11928469
本系列主要结合<PHP和MYSQL WEB开发 第四版>,在阅读中提出自己认为比较重要的一些问题,以加深对知识的了解程度. 1.简短.中等以及冗长风格的表单变量 $name; //简短风格 ...
- Python进阶之函数式编程(把函数作为参数)
什么是函数式编程? 什么是函数式编程? 函数:function 函数式:functional,一种编程范式 函数式编程是一种抽象计算的编程模式 函数≠函数式,比如:计算≠计算机 在计算机当中,计算机硬 ...
- javaScript的2种变量范围有什么不同
1.javascript怎样选中一个checkbox,怎样设置它无效? document.all.cb1[0].disabled = true; 2.js中的3种弹出式消息提醒(警告窗口,确认窗口 ...
- C#学习日志 day10 -------------- problem statement
Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...
- 编写一个程序实现strcmp函数的功能
写自己的strcat函数------→mycmp #include <stdio.h> #include <string.h> #define N 5 int mycmp(ch ...
- C 中注意的小问题
输入:char ch[100],gets(ch); scanf("%d",&in); char ch,ch=getchar(); VC: 所有变量声明放在所有操作前面: ...
- tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中
1.首先还是要将u-boot写入SD卡中从SD卡启动. 使用读卡器将SD插入电脑中,使用umount卸载u盘, fdisk -l显示其挂载点为 /dev/sdb1 切换到/home/bunfly/im ...
- centos6.5 openvpn安装配置
http://m.jb51.net/?host=www.jb51.net&src=http%3A%2F%2Fwww.jb51.net%2Fsoftjc%2F150885.html
- golang win32编程的一个dll坑
例子 package main import ( "github.com/lxn/win" "strconv" "syscall" ) fu ...