标题:想实现一个功能,将1~12个数字分成四组,分别放入a,b,c,d中必须用Map<Stri ...
只看楼主
franksking
Rank: 2
等 级:论坛游民
帖 子:16
专家分:43
注 册:2016-4-25
结帖率:83.33%
已结贴  问题点数:10 回复次数:1 
想实现一个功能,将1~12个数字分成四组,分别放入a,b,c,d中必须用Map<String,ArrayList<String>>这个结
    想实现一个功能,将1~12个数字分成四组,分别放入a,b,c,d中必须用Map<String,ArrayList<String>>这个结构,我想问一下该怎么写呢?如果要是分成六组呢,更多呢?
        public static void main(String[] args)
    {
        String[] array={"a","b","c","d"};
        
        int[] num = new int[12];
        Map<String,ArrayList<String>> map = new HashMap();
        for(int i=0;i<num.length;i++)
        {
            num[i]=i+1;
            //System.out.print(num[i]+" ");
        }
        for(int i=0;i<array.length;i++)
        {
            ArrayList list = new ArrayList();
            for(int j=0;j<num.length;j++)
            {
                if(num[j]<4)
                {
                    list.add(String.valueOf(num[j]));
                    continue;
                }
                else if(num[j]>=4&&num[j]<8)
                {
                    list.add(String.valueOf(num[j]));
                    continue;
                }
                else if(num[j]>=8&&num[j]<10)
                {
                    list.add(String.valueOf(num[j]));
                    continue;
                }
                    
                else
                {
                    list.add(String.valueOf(num[j]));
                    continue;
                }   
                    
                    
                    
            }
            map.put(array[i], list);
            
        }
        
        for(String key:map.keySet())
        {
            System.out.println(key+" "+map.get(key));
        }
        
    }
搜索更多相关主题的帖子: public 
2016-04-27 11:23
DriveK
Rank: 2
等 级:论坛游民
威 望:1
帖 子:4
专家分:45
注 册:2016-4-27
得分:10 
//写的比较粗糙,也不想改了。。。,随便看下吧
public static void main(String[] args) {
        Map<String,ArrayList<String>>  map=allocation(4,12);
        
        for(Map.Entry<String, ArrayList<String>> entry:map.entrySet()){
            String s1=entry.getKey();
            ArrayList<String> list=entry.getValue();
            String s2="";
            for (String s3: list) {
                s2=s2+","+s3;
            }
            System.out.println(s1+"............"+s2.substring(1));
        }
    }
    /**
     *
     * @param GroupNum  分配的组数
     * @param dataLen 分配的数据大小
     * @return
     */

    public static Map<String,ArrayList<String>> allocation(int GroupNum,int dataLen){
        if(dataLen%GroupNum!=0){
            throw new RuntimeException("你输入的数据有问题");
        }
        String[] sArr=new String[GroupNum];
        for(int x=0;x<sArr.length;x++){
            sArr[x]=String.valueOf((char)(97+x));
        }
        ArrayList<ArrayList<String>> list=new ArrayList<ArrayList<String>>();
        Map<String,ArrayList<String>> map=new HashMap<String, ArrayList<String>>();
        ArrayList<String> list1=new ArrayList<String>();
        for(int y=0;y<dataLen;y++){
            if((y%(dataLen/GroupNum)==0)&&y+1!=dataLen){
                 list1.add(String.valueOf(y+1));
            }else{
                list1.add(String.valueOf(y+1));
            }
            if((y+1)%(dataLen/GroupNum)==0){
                list.add((ArrayList<String>) list1.clone());
                list1.clear();
            }
        }
            
            for(int i=0;i<sArr.length;i++){
                map.put(sArr[i], list.get(i));
            }
        return map;
    }
2016-04-27 22:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464179-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.135347 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved