0

In my case I have 4 ArrayList objects as below:

ArrayList<MyProduct> lstStyle;
ArrayList<MyProduct> 2ndStyle;
ArrayList<MyProduct> 3rdStyle;
ArrayList<MyProduct> 4thStyle;

I want to add all the elements in each ArrayList into a new ArrayList called Style.

ArrayList<MyProduct> Style;

Can I do this without looping each ArrayList?

3 Answers 3

3

Use the List#addAll(Collection c) method

Style.addAll(lstStyle);
Style.addAll(2ndStyle);
Style.addAll(3rdStyle);
Style.addAll(4thStyle);

Ofcourse, you need to instantiate all the lists first, otherwise you'll face a NullPointerException.

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

Comments

3
Collections.addAll(Style, lstStyle, 2ndStyle, 3rdStyle, 34thStyle);

1 Comment

Collections.addAll adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.
0

Use addAll method

ArrayList<MyProduct> Style;

 Style.addAll(lstStyle);
 Style.addAll(2ndStyle);
 Style.addAll(3rdStyle);
 Style.addAll(4thStyle);  

Comments

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.