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
-
Possible duplicate of Concatenate multiple arraylists referencesJanTheGun– JanTheGun2016年03月02日 11:26:45 +00:00Commented Mar 2, 2016 at 11:26
-
2did you just ask this duplicate question to answer it for yourself?SomeJavaGuy– SomeJavaGuy2016年03月02日 11:27:41 +00:00Commented 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.coder– coder2016年03月02日 11:28:56 +00:00Commented 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 otherSomeJavaGuy– SomeJavaGuy2016年03月02日 11:30:45 +00:00Commented Mar 2, 2016 at 11:30
3 Answers 3
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
Got the answer:
Here is the way:
myStringList.addAll(newStringList);
answered Mar 2, 2016 at 11:18
1 Comment
coder
@Lathy- Please check the answered duration of mine and his. He has mentioned after I have done that :)
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
lang-java