从ArrayList创建子数组列表

时间:2013-08-30 09:37:54

标签: java arraylist

我有一个像这样的ArrayList

ArrayList<String> listed = new ArrayList<>();

listed.add("sun, water, autumn");
listed.add("moon, wind, winter");
listed.add("venus, fire, summer");

我想创建三个单独的ArrayList,以便第一个包含

("sun, moon, venus")

第二个

("water, wind, fire")

和最后

("autumn, winter, summer")

由于

3 个答案:

答案 0 :(得分:3)

试试这个

    ArrayList<String> listed = new ArrayList<>();
    listed.add("sun, water, autumn");
    listed.add("moon, wind, winter");
    listed.add("venus, fire, summer");
    List<String> subList1=new ArrayList<>();
    List<String> subList2=new ArrayList<>();
    List<String> subList3=new ArrayList<>();
    for(String i:listed){
         subList1.add(i.split(",")[0]);
         subList2.add(i.split(",")[1]);
         subList3.add(i.split(",")[2]);
    }

答案 1 :(得分:0)

假设您有固定格式

    List<String> list1 =  Arrays.asList(listed.get(0).split(",")); 
    List<String> list2 =  Arrays.asList(listed.get(1).split(",")); 
    List<String> list3 =  Arrays.asList(listed.get(2).split(",")); 

答案 2 :(得分:0)

ArrayList finalList  = new ArrayList();
for(String s : listed) {
    finalList.add(Arrays.asList(s.split(",")));  
}