Java:集合for高级循环遍历
增强for循环:
格式:for(变量数据类型 要遍历的变量 :元素所在数组(集合)名称)
也即 for(Type element: array或collection)
使用foreach遍历集合:
只能获取集合中的元素,不能对集合进行操作。
而迭代器Iterator除了可以遍历,还可以对集合中的元素遍历时进行remove操作。
如果使用ListIterator还可以在遍历过程中进行增删改查的动作。
//例子1:
import java.util.*;
class Foreach
{
public static<T> void sop(T t)
{
System.out.println(t);
}
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>(); al.add("abc");
al.add("bcd");
al.add("kef"); //1、传统for循环遍历(按角标获取)
for(int i=0;i<al.size();i++)
{
sop(al.get(i));
}
sop("\n"); //2、for循环遍历(迭代器)
for(Iterator<String> it2 = al.iterator();it2.hasNext();)
{
sop(it2.next());
}
sop("\n"); /* //for循环遍历(vector集合的枚举)
for(Enumeration<String> en = al.elements();en.hasMoreElements();)
{
sop(en.nextElement());
}
sop("\n");
*/
//3、for循环增强遍历
for(String s: al)
{
sop(s);
}
}
}
在Map集合中使用for高级遍历(foreach)
//例子2:
import java.util.*;
class Foreach2
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
Map<String,Integer> hm = new HashMap<String,Integer>(); hm.put("a",1);
hm.put("b",2);
hm.put("c",3); Set<String> keyset = hm.keySet();
Set<Map.Entry<String,Integer>> entryset = hm.entrySet(); //转化为Set集合后,直接用迭代器获取元素(方法:keySet())
Iterator<String> it1 = keyset.iterator();
while(it1.hasNext())
{
String str = it1.next();
Integer in = hm.get(str);
sop("str:"+str+" "+"in:"+in);
}
sop("---------------------"); //转化为Set集合后,用for循环高级遍历获取元素
for(String str: keyset)
{
sop("str:"+str+"::"+"in:"+hm.get(str));
}
sop("---------------------"); //转化为Set集合后,直接用迭代器获取元素(方法:entrySet())
Iterator<Map.Entry<String,Integer>> it2 = entryset.iterator();
while(it2.hasNext())
{
Map.Entry<String,Integer> me = it2.next();
String str = me.getKey();
Integer in = me.getValue();
sop("str:"+str+" "+"in:"+in);
}
sop("---------------------"); //转化为Set集合后,用for循环高级遍历获取元素
for(Map.Entry<String,Integer> me: entryset)
{
sop("str:"+me.getKey()+"....."+"in:"+me.getValue());
}
}
}
Java:集合for高级循环遍历的更多相关文章
- java 集合之Arraylist的遍历及排序
最近培训是先学习java基础 从最基本的开始学起 因为今天刚刚开博客 要把上周的一些重点内容归纳一下 1.Arraylist常用遍历以及排序 import java.util.ArrayList; i ...
- java集合的三种遍历方式
import java.util.ArrayList; import java.util.Collection;import java.util.Iterator;public class Home ...
- java集合-遍历arraylist-for循环-从指定下标开始遍历-for的用法
转载:http://www.9191boke.com/blogdetails/681220549.html java集合的for循环遍历有多种方式,但是都是从下标0开始遍历,有时会有从中间下标开始遍历 ...
- Java集合——Map接口
1.定义 Map用于保存存在映射关系<key,value>的数据.其中,key值不能重复(使用equals()方法比较),value值可以重复 2.方法 V put(key,value) ...
- Java 集合、Iterator迭代器、泛型等
01集合使用的回顾 A:集合使用的回顾 a.ArrayList集合存储5个int类型元素 public static void main(String[] args) { ArrayList<I ...
- Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]
Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...
- java中数组、集合、字符串之间的转换,以及用加强for循环遍历
java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...
- Android java程序员必备技能,集合与数组中遍历元素,增强for循环的使用详解及代码
Android java程序员必备技能,集合与数组中遍历元素, 增强for循环的使用详解及代码 作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 For ...
- Java之Iterator接口(遍历单列集合的迭代器)
Iterator接口概述 在程序开发中,经常需要遍历集合中的所有元素.针对这种需求,JDK专门提供了一个接口java.util.Iterator . Iterator 接口也是Java集合中的一员,但 ...
随机推荐
- Android -- TabHost、Fragment、状态保存、通信
工程结构 TabAFm到Ta ...
- Linux学习之路--启动VNC服务
我的Linux是Fedora 13,安装方法如下: 1.打开终端,执行 # yum install -y tigervnc tigervnc-server 2.编辑/etc/sysconfi/vncs ...
- fstab文件
格式: 文件系统 挂载点 格式 挂载文件系统选项 备份选项 自检选项 1.指定挂载的设备或者远程文件系统,比如普通的挂载设备/dev/sda1, 对于nfs格式:<host>:<di ...
- VLC编译问题
在Ubuntu下编译VLC源代码生成的VLC无法播放Youtube视频(比如https://www.youtube.com/watch?v=mDp-ABzpRX8) 错误提示如下: zlf@ubunt ...
- mysql 触发器的使用(备忘)
触发器创建语法四要素: 1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/del ...
- Java 7 中 NIO.2 的使用——文件递归操作
众所周知,递归编程是一项有争议的技术,因为它需要大量的内存,但是它能简化一些编程任务.基本上,一个递归操作都是程序调用自己传递参数修改的值或者参数传递到当前的程序循环中.递归编程通常用来计算阶乘斐波那 ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- tomcat集群 (自带Cluster集群)
不用借助其他任何工具,tomcat自身就可以实现session共享,实现集群.以下为大概步骤 1,如果是在同一台机器上,请保持多个tomcat端口(一个tomcat对应三个端口)不相同:如果是不同机器 ...
- 7-Highcharts曲线图之分辨带
<!DOCTYPE> <html lang='en'> <head> <title>7-Highcharts曲线图之分辨带</title> ...
- frequentism-and-bayesianism-chs-iii
frequentism-and-bayesianism-chs-iii 频率主义 vs 贝叶斯主义 III:置信(Confidence)与可信(Credibility),频率主义与科学,不能混为一 ...