javacodex.com
Java Examples
Java Examples
memu home questions

Add Contents Of One ArrayList to Another

This java example demonstrates how to add the contents of one arrayList to another.

Source: (CombineArrayList.java)

import java.util.ArrayList;
 
public class CombineArrayList { 
  public static void main(String[] args) { 
    ArrayList<String> boys = new ArrayList<String>();
    boys.add("Mike");
    boys.add("Tom");
    boys.add("Bill");
    ArrayList<String> girls = new ArrayList<String>();
    girls.add("Mary");
    girls.add("Sue");
    girls.add("Amy");
 
    System.out.println("Boys List: "+ boys);
    System.out.println("Girls List: "+ girls);
 
    boys.addAll(girls);    
    System.out.println("New Boys List: "+ boys);
  } 
}
 

Output:

# java CombineArrayList 
Boys List: [Mike, Tom, Bill]
Girls List: [Mary, Sue, Amy]
New Boys List: [Mike, Tom, Bill, Mary, Sue, Amy]

Contact: javacodex@yahoo.com