首页 新闻 搜索 专区 学院

数据的拼装问题,,无从下手

0
悬赏园豆:5 [待解决问题]

数组A[{2017,20},{2017,22}{2017,25}{2017,26}{2017,28},{2018,20}{2018,21}]

如何拼装成{2017:['20','22','25','26','28'],2018:['20','21']}

海染蓝了天LE的主页 海染蓝了天LE | 初学一级 | 园豆:26
提问于:2018-01-16 09:39
< >
分享
所有回答(3)
1

[{2017,20},{2017,22}{2017,25}{2017,26}{2017,28},{2018,20}{2018,21}]

哪种语言这样表达一个数组的?

西漠以西 | 园豆:1670 (小虾三级) | 2018-01-16 09:54
0
 1     public static void main(String[] args) {
 2         Object[] A = new Object[1];
 3         A[0]= "{2017,20},{2017,22}{2017,25}{2017,26}{2017,28},{2018,20}{2018,21}";
 4         String regex = (String)A[0];
 5         String[] date = regex.split("\\},\\{|\\}\\{|\\{|\\}");
 6         List<String> seventeen = new ArrayList<String>();
 7         List<String> eighteen = new ArrayList<String>();
 8         for(int i =0;i<date.length;i++) {
 9             if(date[i].contains("2017")) {
10                 String[] day = date[i].split(",");
11                 seventeen.add(day[1]);
12             }
13             if(date[i].contains("2018")) {
14                 String[] day = date[i].split(",");
15                 eighteen.add(day[1]);
16             }
17         }
18         StringBuffer seventeenSB = new StringBuffer("2017:[");
19         seventeen.forEach(e -> {
20             seventeenSB.append("'"+e+"',");
21         });
22         StringBuffer eighteenSB = new StringBuffer("2018:[");
23         eighteen.forEach(e -> {
24             eighteenSB.append("'"+e+"',");
25         });
26         seventeenSB.delete(seventeenSB.length()-1, seventeenSB.length());
27         eighteenSB.delete(eighteenSB.length()-1, eighteenSB.length());
28         seventeenSB.append(']');
29         eighteenSB.append(']');
30         String str = new String("{"+seventeenSB+","+eighteenSB+"}");
31         System.out.println(str);
32     }

有些简陋,算是给你个思路吧。

勤劳的杯子 | 园豆:178 (初学一级) | 2018-01-16 17:24
0

js数组?

求知若饥,虚心若愚 | 园豆:25 (初学一级) | 2018-01-22 06:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册