0

I have an ArrayList such as this one

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

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

and I would like to create three separate ArrayList so that first one contains

("sun, moon, venus")

second one

("water, wind, fire")

and last

("autumn, winter, summer")

Thanks

1
  • Thanks you all for your very useful help Commented Aug 30, 2013 at 9:55

3 Answers 3

3

Try this

    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]);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you have a Fixed format

    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(",")); 

Comments

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

2 Comments

When posting answers, it is really a great thing to explain the code that you are posting. Even if it works perfectly and it is tested, adding the explanation of WHY it works is a great thing to do.
Thanks Fluffeh for your valuable comment. I will ensure this going forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.