首页 > 其他 > 详细

十五周

时间:2020-06-14 20:26:16      阅读:45      评论:0      收藏:0      [点我收藏+]
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
public class text {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("亚亚", "Q妹");
        map.put("菲菲", "Q妹");
        map.put("欧欧", "Q仔");
        map.put("亚亚", "Q妹");
        map.put("美美", "Q妹");
        System.out.println("使用Iterator遍历,所有企鹅的昵称和品种分别是:");
        Set<String> keyset = map.keySet();
        Iterator<String> it = keyset.iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = map.get(key);
            System.out.println(key + "     " + value);
        }
 
        System.out.println("使用foreach遍历,所有企鹅的昵称和品种分别是:");
        for (String in : map.keySet()) {
            String str = map.get(in);
            System.out.println(in + "     " + str);
        }
    }
 
}
技术分享图片
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
public class text2 {
public static void main(String[] args) {
 
         
        Map<Integer,Penguin> map1 = new HashMap<Integer,Penguin>();
        Penguin a1 = new Penguin("欧欧","Q仔");
        Penguin a2 = new Penguin("亚亚","Q妹");
        Penguin a3 = new Penguin("美美","Q妹");
        Penguin a4 = new Penguin("菲菲","Q妹");
        map1.put(1, a1);
        map1.put(2, a2);
        map1.put(3, a3);
        map1.put(4, a4);
        System.out.println("使用foreach遍历,所有企鹅的昵称和品种分别是:");
        for (Integer in : map1.keySet()) {
            Penguin str = map1.get(in);
            System.out.println(in + "     " + str.getName()+ "     " +str.getSex());
        }
        System.out.println("使用Iterator遍历,所有企鹅的昵称和品种分别是:");
        Set<Integer> key = map1.keySet();
        Iterator<Integer> it1 = key.iterator();
        while (it1.hasNext()) {
            Integer key1 = it1.next();
            Penguin value = map1.get(key1);
            System.out.println(key1 + "     " + value.getName()+ "     " +value.getSex());
        }
         
    }
 
}

技术分享图片

 

十五周

原文:https://www.cnblogs.com/tianzhiyuan/p/13126217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!