增强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. PSP0表格二

    一 项目计划日志 周活动总结表 姓名: 陆宇 日期:2015. 3. 21 日期       任务 听课 编写程序 阅读课本 准备考试 日总计/(min) 周日 60 30 90 周一 300 0 1 ...

  2. 对MVC的理解

    摘要:本文主要谈到了对PHP开发中MVC开发模式的理解. 当用户通过url触发命令时,例如url=http://control.blog.sina.com.cn/admin/article/artic ...

  3. makefile常用函数

    标签(空格分隔): makefile 1.字符串替换和分析函数 $(subst from,to,text) #在文本"text"中使用"to"替换每一处&quo ...

  4. Oracle数据库中文乱码问题

    最近碰到Oracle乱码问题,刚开始甚是头疼,以前在合肥出差的时候,这种问题也碰到过,当时直接抛给了“乌压压一片”(一个搞数据的同事儿),这次没办法躲过,只好硬着头皮上.虽然我这次碰到的是Oracle ...

  5. 三级联动(ajax)

    <body> <div id="zhuti"></div> </body><script type="text/ja ...

  6. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  7. 【python】正则表达式

    参考资料:http://deerchao.net/tutorials/regex/regex.htm 1.正则表达式基础 2.python 正则表达式 1.正则表达式基础 元字符: 其他语法: (1) ...

  8. poj 2342 Anniversary party

    题目链接:http://poj.org/problem?id=2342 题意:读题很容易懂,这里不做介绍. 解法:树形DP之路的第一道题. #include<iostream> #incl ...

  9. 【BZOJ】【4027】【HEOI2015】兔子与樱花

    贪心 树上贪心问题……跟APIO2015练习赛的C很像啊…… 我的思路是:从叶子向上考虑,令a[x]表示x这个节点上樱花数量与儿子个数的和(即对于任意的x,都有$a[x]\leq m$)每次从儿子的a ...

  10. NYOJ-32 组合数 AC 分类: NYOJ 2013-12-30 07:42 189人阅读 评论(0) 收藏

    #include<stdio.h> int num[100]; int pnum(int n,int v); int mv=0; int main(){ int n,v; scanf(&q ...