用户操作
[即时聊天] [发私信] [加为好友]
xuxuyzID:high_land
2128次访问,排名2万外好友0人,关注者4
3年java程序员,乒乓球,沈大,东大,双学历
high_land的文章
原创 1 篇
翻译 0 篇
转载 1 篇
评论 24 篇
最近评论
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Hashtable的while循环,HashMap、List的嵌套循环。 收藏

     | 旧一篇: Mail送信(JFrame类)

    package com.util;

    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    public class HashMapAndListAndHashtable {
       
        /**
         *      -->Map
         * List -->Map -->List-->Map
         *
         * for list  ---  while map
         *
         * @throws Exception
         */
        public void listAndHashMap() throws Exception{
           
            try{
               
                Map mapLst0 = new HashMap();
                mapLst0.put("testkey","testName");
                Map mapLst1 = new HashMap();
                mapLst1.put("ESTIMDEF","評価値1");
                mapLst1.put("ESTIMNAME","評価名1");           
                Map mapLst2 = new HashMap();
                mapLst2.put("ESTIMDEF","評価値2");
                mapLst2.put("ESTIMNAME","評価名2");
                   
                Map mapLst3 = new HashMap();
                mapLst3.put("ESTIMDEF","評価値3");
                mapLst3.put("ESTIMNAME","評価名3");
                Map mapLst4 = new HashMap();
                mapLst4.put("ESTIMDEF","評価値4");
                mapLst4.put("ESTIMNAME","評価名4");
                   
                List lstMa1 = new ArrayList();
                lstMa1.add(mapLst1);
                lstMa1.add(mapLst2);
                   
                List lstMa2 = new ArrayList();
                lstMa2.add(mapLst3);
                lstMa2.add(mapLst4);
                   
                Map map = new HashMap();
                map.put("key",mapLst0);
                map.put("lstMa1",lstMa1);
               
                Map map1 = new HashMap();           
                map1.put("key",mapLst0);
                map1.put("lstMa1",lstMa2);
                   
                List lst = new ArrayList();
                lst.add(map);
                lst.add(map1);
               
                Map mapfor = null;
                Map mapfor1 = null;
                List lstfor = null;
                String key = null;
                String key1 = null;
                Iterator itor = null;
                Iterator itor1 = null;

                //one line name
                if(lst != null || lst.size() != 0){
                       
                    mapfor = (Map)lst.get(0);
                    itor = mapfor.keySet().iterator();                   
                    while(itor.hasNext()){
                           
                        key = (String) itor.next();                   
                        if (key.startsWith("lstMa1")) {
                           
                            lstfor = (List)mapfor.get(key);                       
                            for(int j = 0; j < lstfor.size(); j++){
                               
                                mapfor1 = (Map)lstfor.get(j);                           
                                itor1 = mapfor1.keySet().iterator();                   
                                while(itor1.hasNext()){
                                   
                                    key1 = (String)itor1.next();                               
                                    if(key1.startsWith("ESTIMNAME")){
       
                                        System.out.println("mapfor1 name = " + mapfor1.get(key1));
                                    }
                                }
                            }
                        }
                    }               
                }

                //many lines value
                for(int i = 0; i < lst.size(); i++){
                       
                    mapfor = (Map)lst.get(i);
                    itor = mapfor.keySet().iterator();                   
                    while(itor.hasNext()){
                           
                        key = (String) itor.next();                   
                        if (key.startsWith("lstMa1")) {
                           
                            lstfor = (List)mapfor.get(key);
                            for(int j = 0; j < lstfor.size(); j++){
                               
                                mapfor1 = (Map)lstfor.get(j);
                                itor1 = mapfor1.keySet().iterator();
                                while(itor1.hasNext()){
                                   
                                    key1 = (String)itor1.next();
                                    if(key1.startsWith("ESTIMDEF")){

                                        System.out.println("mapfor1 value = " + mapfor1.get(key1));
                                    }
                                }
                            }
                        }
                    }
                }
               
            }catch (Exception e){
                e.printStackTrace();   
            }

        }

        /**
         * while hashtable
         */
        public void hashtable(){

            Map mapLst1 = new HashMap();
            mapLst1.put("ESTIMDEF","評価値1");
            mapLst1.put("ESTIMNAME","評価名1");
                      
            Map mapLst2 = new HashMap();
            mapLst2.put("ESTIMDEF","評価値2");
            mapLst2.put("ESTIMNAME","評価名2");
           
            List lst = new ArrayList();
            lst.add("xuyz");
            lst.add("lit");
            lst.add("aaa");
           
            Hashtable hashtable = new Hashtable();
            hashtable.put("mapLst1",mapLst1);
            hashtable.put("mapLst2",mapLst2);
            hashtable.put("lst",lst);
           
            String key = null;

            //get enumModigy
            Enumeration enumModigy = hashtable.keys();

            while (enumModigy.hasMoreElements()) {

                //get key
                key = (String)enumModigy.nextElement();

                Object obj = hashtable.get(key);
               
                System.out.println("obj = " + obj);

            }

        }
        /**
         * main()
         * @param args
         */
        public static void main(String[] args) {
           
            HashMapAndListAndHashtable test = new HashMapAndListAndHashtable();
            try{
                //
                test.listAndHashMap();
               
                System.out.println("#############################");
                //
                test.hashtable();
               
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    发表于 @ 2005年12月20日 14:29:00|编辑

     | 旧一篇: Mail送信(JFrame类)

    评论:没有评论。

    Csdn Blog version 3.1a
    Copyright © high_land