coder
coder

Reputation: 13248

Adding one arraylist to another

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 ?

Upvotes: 0

Views: 2806

Answers (3)

Jagjit Singh
Jagjit Singh

Reputation: 1969

try this.Hope it helps

1. myStringList.addAll(newStringList);
2. you can loop and add to other.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

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);

Upvotes: 4

coder
coder

Reputation: 13248

Got the answer:

Here is the way:

 myStringList.addAll(newStringList);

Upvotes: 0

Related Questions