编程语言
首页 > 编程语言> > Java String 转成 二位数组

Java String 转成 二位数组

作者:互联网

...

package str;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class forma {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Map<String, Object>> containsList = new ArrayList<Map<String,Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "1");
        map1.put("name", "mysteriousKiller");
        map1.put("age", "11");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "2");
        map2.put("name", "feel");
        map2.put("age", "12");
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", "3");
        map3.put("name", "real");
        map3.put("age", "13");
        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("id", "4");
        map4.put("name", "killer");
        map4.put("age", "14");
        Map<String, Object> map5 = new HashMap<String, Object>();
        map5.put("id", "5");
        map5.put("name", "secret");
        map5.put("age", "15");
        Map<String, Object> map6 = new HashMap<String, Object>();
        map6.put("id", "6");
        map6.put("name", "mysterious");
        map6.put("age", "16");
        Map<String, Object> map7 = new HashMap<String, Object>();
        map7.put("id", "7");
        map7.put("name", "clamness");
        map7.put("age", "17");
        Map<String, Object> map8 = new HashMap<String, Object>();
        map8.put("id", "8");
        map8.put("name", "killer");
        map8.put("age", "18");
        
        Map<String, Object> map9 = new HashMap<String, Object>();
        map9.put("id", "9");
        map9.put("name", "killer");
        map9.put("age", "19");
        
        containsList.add(map1);
        containsList.add(map2);
        containsList.add(map3);
        containsList.add(map4);
        containsList.add(map5);
        containsList.add(map6);
        containsList.add(map7);
        containsList.add(map8);
        containsList.add(map9);
        
        //System.out.println(containsList);
        System.out.println("-------------------");
        /*
        int a = 0 ;
        int b = 0 ; 
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> list2 = new ArrayList<String>();
        for (Map<String, Object> map : containsList) {
            if(map.get("name").equals("killer")){ // 一般不会开始就进
                list.add(list2);
                list2 = new ArrayList<String>();
                list2.add(map.get("id").toString());
            }else{
                list2.add(map.get("id").toString());  
            }
        }
        if(list2.size()>0){
            list.add(list2); //加上最后一个
        }
        
        System.out.println(list);
        */
        
        
        /*
        for (Map<String, Object> map : containsList) {
            System.out.println(map);
            for (String s : map.keySet()) {
                System.out.println(map.get(s) + "  ");
            }
        }*/
        
        int a = 0 ;
        int b = 0 ;
        String jl = "["; 
        for (Map<String, Object> map : containsList) {
            System.out.println(map); 
            b+=1;
            if(map.get("name").equals("killer")){ // 一般不会开始就进
                a++; 
                jl = jl.substring(0,jl.length()-1); // 去掉后面的逗号
                
                jl += "],["+map.get("id")+",";
                //break;
                continue;
            }else{ 
                jl+= map.get("id")+",";
                System.out.println(map.get("name"));
                System.out.println(map.keySet());
            }
        } 
        jl = jl.substring(0,jl.length()-1);
        jl+="]";  
        jl = "["+jl+"]";
        System.out.println(jl);
        
         
        String[] s1 = jl.replaceAll("],", "]#").split("#");
        String[][] arr = new String[s1.length][];
        for(int i=0;i<arr.length;i++) {
            String[] s2 = s1[i].split(",");
            arr[i] = new String[s2.length];
            for(int j=0;j<s2.length;j++) {
                arr[i][j] = s2[j].replaceAll("\\[|\\]", "");
            }
        }
         
        for(String[] i:arr) {
            System.out.println(Arrays.toString(i));
        }
    }

}

 

标签:map,Java,String,jl,add,数组,put,new,containsList
来源: https://www.cnblogs.com/mysterious-killer/p/11165512.html