0

Here are my two Arraylists and how do I add one to the other

private ArrayList<String> myStringList= new ArrayList<String>();
private ArrayList<String> newStringList= new ArrayList<String>();

I would like to add all the strings which are in myStringList to newStringList.

Can anyone help me with this ?

asked Mar 2, 2016 at 11:16
4
  • Possible duplicate of Concatenate multiple arraylists references Commented Mar 2, 2016 at 11:26
  • 2
    did you just ask this duplicate question to answer it for yourself? Commented Mar 2, 2016 at 11:27
  • @Kevin. Not really :) but found lately the mistake I have done and finally answered my own question here so that someone might go through it. Commented Mar 2, 2016 at 11:28
  • @coder ok, it just looks confusing since the question and the answer are just 1 minute apart from each other Commented Mar 2, 2016 at 11:30

3 Answers 3

4

You can use the constructor which takes a collection like

private List<String> newStringList= new ArrayList<>(myStringList);

Or call List.addAll(Collection) like

newStringList.addAll(myStringList);
answered Mar 2, 2016 at 11:18

Comments

0

Got the answer:

Here is the way:

 myStringList.addAll(newStringList);
answered Mar 2, 2016 at 11:18

1 Comment

@Lathy- Please check the answered duration of mine and his. He has mentioned after I have done that :)
0

try this.Hope it helps

1. myStringList.addAll(newStringList);
2. you can loop and add to other.
answered Mar 2, 2016 at 11:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.