-2

I have a source list of type ArrayList<ArrayList<String>>. I want to put all the strings from the source list into a destination list of type ArrayList<String>. How do I do this?

Example:

sourceList = { {"a","b"}, {"c","b"} }
destinationList = { "a","b","c","d" }
4
  • 4
    And your question is? Commented Jun 15, 2018 at 13:30
  • Can't get what you are asking? Commented Jun 15, 2018 at 13:33
  • Tell us what is your problem ... and also peace of code Commented Jun 15, 2018 at 13:33
  • 2
    @Vishali Neither can we Commented Jun 15, 2018 at 13:34

1 Answer 1

2

You can use for-each and addAll():

for (ArrayList<String> subList : listOfLists) {
    listofStrings.addAll(subList);
}

See: https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html https://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll(java.util.Collection)

Sign up to request clarification or add additional context in comments.

3 Comments

This is arraylist.you are adding arraylist values.i want arrayofarrayList
If I use arrayofarrayList I am getting only last index in my arraylist
@Vishali, you need to post some of your code so that we can help you. Please edit your question and include the relevant code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.