加强for循环

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/*
jdk1.5出现的新特性---->增强for循环

增强for循环的作用: 简化迭代器的书写格式。(注意:增强for循环的底层还是使用了迭代器遍历。)

增强for循环的适用范围: 如果是实现了Iterable接口的对象或者是数组对象都可以使用增强for循环。

增强for循环的格式:
 	
 	for(数据类型  变量名  :遍历的目标){
 	
 	}

增强for循环要注意的事项:
	1. 增强for循环底层也是使用了迭代器获取的,只不过获取迭代器由jvm完成,不需要我们获取迭代器而已,所以在使用增强for循环变量元素的过程中不准使用集合
	对象对集合的元素个数进行修改。
	2. 迭代器遍历元素与增强for循环变量元素的区别:使用迭代器遍历集合的元素时可以删除集合的元素,而增强for循环变量集合的元素时,不能调用迭代器的remove方法删除元素。
	3. 普通for循环与增强for循环的区别:普通for循环可以没有变量的目标,而增强for循环一定要有变量的目标。

 

 */
public class demo18{
	
	public static void main(String[] args) {
		HashSet<String> set = new HashSet<String>();//<String> 指定集合中存储的数据类型
		//添加元素
		set.add("狗娃");
		set.add("狗剩");
		set.add("铁蛋");
		
		/*
		//使用迭代器遍历Set的集合.
		Iterator<String> it  = set.iterator();
		while(it.hasNext()){
			String temp = it.next();
			System.out.println("元素:"+ temp);
			it.remove();
		}
		
		
		//使用增强for循环解决
		for(String item : set){
			System.out.println("元素:"+ item);
			
		}
		
		
		
		
		int[] arr = {12,5,6,1};
		
	 	普通for循环的遍历方式
	 	for(int i =  0 ; i<arr.length ; i++){
			System.out.println("元素:"+ arr[i]);
		}
		
		//使用增强for循环实现
		for(int item :arr){
			System.out.println("元素:"+ item);
		}
		
		
		
		//需求: 在控制台打印5句hello world.
		for(int i = 0 ; i < 5; i++){
			System.out.println("hello world");
		}
		*/
		
		//注意: Map集合没有实现Iterable接口,所以map集合不能直接使用增强for循环,如果需要使用增强for循环需要借助于Collection
		// 的集合。
		HashMap<String, String> map = new HashMap<String, String>();
	/*  set集合中的      boolean containsAll(Collection<?> c);
	 Adds all of the elements in the specified collection to this set if*/
		map.put("002","李四");
		map.put("003","王五");
		map.put("004","赵六");
		Set<Map.Entry<String, String>> entrys = map.entrySet();//Entry属于map接口中的一个方法,map可写,也可不写,但是最好写上,这符合代码的规范
		for(Map.Entry<String, String> entry  :entrys){
			System.out.println("键:"+ entry.getKey()+" 值:"+ entry.getValue());
		}
		
		
	}

}  /*Set<Map.Entry<K,V>> es = entrySet;
//return es != null ? es : (entrySet = new EntrySet());
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值