增强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. 2015最新百度搜索引擎(seo优化)排名算法

    多少年来,对于弄清百度排名算法成为了一代又一代站长的最高目标.随着百度推出了搜索引擎网页质量**,直接揭开了神秘的百度排名算法,这是作为站长福音啊.现在小编就来为大家介绍一下. 首先想要得到直接需要的 ...

  2. 使用python远程登录

    最近要使用python做一个在web上管理交换机的程序,需要远程登录,就查了点资料,由于还没有搞到交换机,就先用自己的机器测试一下. 首先python的标准库中包含telnet,用起来也很方便,查看一 ...

  3. python-根据字符串动态生成对象eval

    # -*- coding: utf-8 -*- stock1={ 'stockName':"沈阳机床", ", 'averagePrice_yesterday':34.0 ...

  4. 【CentOS】安装jdk

    问题描述:       jdk安装与配置   (1)查看是否安装jdk --CentOS默认自带openjdk         先查看 rpm -qa | grep java         显示如下 ...

  5. GS初始化

    开启GameServer模式 init函数,现在看看这个大函数干什么的 //这个init也是GameServerUI里面调的,这个线程其实就做了一些初始化的工作,其实这里面没有什么主不主线程,都是在一 ...

  6. Linux查看日志命令

    tail -f /var/log/apport.log more /var/log/xorg.0.log cat /var/log/mysql.err less /var/log/messages g ...

  7. LCA 笔记

    简述这几天的LCA 心得: LCA有两种做法:离线 or 在线 先学的离线: 原理博客很多. 我写得两道题,已经模板. HDU:http://acm.hdu.edu.cn/showproblem.ph ...

  8. SOA之(2)——SOA架构基础概念与设计框架

    SOA的设计框架 设计框架与架构相关的概念紧密相连,原则.模式和架构始终是与设计共舞的. SOA服务设计的原则中记录了一个基础的设计框架: 设计特性(Design Characteristic)——由 ...

  9. POJ 1650

    #include <iostream> #include <cmath> //wo de 编译器里的这个abs的功能不能用啊! using namespace std; int ...

  10. Linux下搭建Android NDK , Linux 驱动开发环境

    Eclispe Luna(4.4):http://www.eclipse.org/downloads/ CDT :http://www.eclipse.org/cdt/downloads.php AD ...