增强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高级循环遍历的更多相关文章

  1. java 集合之Arraylist的遍历及排序

    最近培训是先学习java基础 从最基本的开始学起 因为今天刚刚开博客 要把上周的一些重点内容归纳一下 1.Arraylist常用遍历以及排序 import java.util.ArrayList; i ...

  2. java集合的三种遍历方式

    import java.util.ArrayList;  import java.util.Collection;import java.util.Iterator;public class Home ...

  3. java集合-遍历arraylist-for循环-从指定下标开始遍历-for的用法

    转载:http://www.9191boke.com/blogdetails/681220549.html java集合的for循环遍历有多种方式,但是都是从下标0开始遍历,有时会有从中间下标开始遍历 ...

  4. Java集合——Map接口

    1.定义 Map用于保存存在映射关系<key,value>的数据.其中,key值不能重复(使用equals()方法比较),value值可以重复 2.方法 V  put(key,value) ...

  5. Java 集合、Iterator迭代器、泛型等

    01集合使用的回顾 A:集合使用的回顾 a.ArrayList集合存储5个int类型元素 public static void main(String[] args) { ArrayList<I ...

  6. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

  7. java中数组、集合、字符串之间的转换,以及用加强for循环遍历

    java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...

  8. Android java程序员必备技能,集合与数组中遍历元素,增强for循环的使用详解及代码

    Android java程序员必备技能,集合与数组中遍历元素, 增强for循环的使用详解及代码 作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 For ...

  9. Java之Iterator接口(遍历单列集合的迭代器)

    Iterator接口概述 在程序开发中,经常需要遍历集合中的所有元素.针对这种需求,JDK专门提供了一个接口java.util.Iterator . Iterator 接口也是Java集合中的一员,但 ...

随机推荐

  1. 【每日scrum】NO.2

    1.今天找到了铁大电子地图. 2.需求分析未完成,进度有点慢.

  2. Careercup - Microsoft面试题 - 5673934611546112

    2014-05-10 23:26 题目链接 原题: what is the best,worst and average case complexity for fibonacci no.s ..ex ...

  3. Hibernate映射类型对照表

    Hibernate映射类型对照表 java类型  Hibernate映射类型  SQL类型 java.math.BigDecimal big_decimal numeric byte[] binary ...

  4. Restful API 最佳实践 (理论篇)

    参考: http://www.ibm.com/developerworks/cn/web/1103_chenyan_restapi/ 规划好 资源标示结构 和 URI模式, 是API设计成功的关键 原 ...

  5. git manual

    git init                                                  # 初始化本地git仓库(创建新仓库) git config --global us ...

  6. Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏

    #include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...

  7. asp.net 分布式缓存

    之前Velocity已被 集成到App Fabric(包含有WCF监控==)中.   网络Velocity使用大多是针对老版本:  老版本的下载地址:  http://www.microsoft.co ...

  8. .NET中JSON的序列化和反序列化

    .NET 中有两种方法进行JSON的操作分别需要引用不同的命名空间 1.System.Runtime.Serialization.Json(System.Runtime.Serialization.d ...

  9. HTML5程序设计--SVG

    SVG(Scalable Vector Graphics):可缩放矢量图形,一种二维图形表示语言. 借助SVG,我们可以实现很多同Canvas API类型的绘制操作,但在Canvas元素上绘制文本的时 ...

  10. JavaScript文件应该放在网页的什么位置

    JavaScript文件应该放在网页的什么位置 http://www.lihuai.net/qianduan/js/352.html 在<良好的JavaScript编程习惯>系列教程的第三 ...