zoukankan      html  css  js  c++  java
  • list,set,map集合的遍历

    package com.test;
    
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    /**
     * list集合遍历  set集合遍历  hashmap集合遍历
     * 
     * @author 18430
     *
     */
    
    public class Test {
    	public static void main(String[] args) {
    
    		Student student1 = new Student("zhangsan", 90);
    		Student student2 = new Student("lisi", 78);
    		Student student3 = new Student("wangwu", 56);
    
    		ArrayList<Student> list = new ArrayList<Student>();
    		list.add(student1);
    		list.add(student2);
    		list.add(student3);
    
    		for (int i = 0; i < list.size(); i++) {
    			System.out.print(list.get(i));
    		}
    		System.out.println();
    		System.out.println("for循环遍历list================================================");
    
    		//
    		Iterator iterator = list.iterator();
    		while (iterator.hasNext()) {
    			System.out.print(iterator.next());
    		}
    
    		System.out.println();
    		System.out.println("迭代器遍历list================================================");
    
    		//
    		for (Student student : list) {
    			System.out.print(student);
    		}
    
    		System.out.println();
    		System.out.println("foreach遍历list================================================");
    
    		HashSet<Student> set = new HashSet<>();
    		set.add(student1);
    		set.add(student2);
    		set.add(student3);
    
    		for (Student student : set) {
    			System.out.print(student);
    		}
    
    		System.out.println();
    		System.out.println("foreach遍历set================================================");
    
    		Iterator iterator1 = set.iterator();
    		while (iterator1.hasNext()) {
    			System.out.print(iterator1.next());
    		}
    
    		System.out.println();
    		System.out.println("迭代器遍历set================================================");
    
    		HashMap map = new HashMap();
    		map.put("1", student1);
    		map.put("2", student2);
    		map.put("3", student3);
    
    		Iterator iterator3 = map.keySet().iterator();
    		while (iterator3.hasNext()) {
    			System.out.println(map.get(iterator3.next()));
    		}
    
    		System.out.println();
    		System.out.println("map集合的遍历================================================");
    
    	}
    
    }
    

      

  • 相关阅读:
    nohup
    wonder vscode plugins
    myhome vscode plugins
    virtural machine eth1
    单片机电子时钟的设计(期末课程设计)
    解决Eclipse中更改HTML页面后,浏览器查看页面无变化
    ASP.NET 中的 Session对象
    windows下mysql数据库导入导出
    TP5.1分表,partition分表实例,根据自增主键水平分表
    PHP操作mysql数据库分表的方法
  • 原文地址:https://www.cnblogs.com/yanweichen/p/9281594.html
Copyright © 2011-2022 走看看