Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList addAll

This Java Example shows how to add all elements of Collection to Java ArrayList using addAll method.
  1. import java.util.ArrayList;
  2.  
  3. //Java ArrayList addAll
  4.  
  5. /*
  6.  * This Java Example shows how to add all elements of Collection to
  7.  * Java ArrayList using addAll method.
  8.  */
  9.  
  10. public class JavaArrayListAddAll {
  11.  
  12.     public static void main(String args[]){
  13.        
  14.         ArrayList<String> aListBasicColors = new ArrayList<String>();
  15.         aListBasicColors.add("Red");
  16.         aListBasicColors.add("Green");
  17.         aListBasicColors.add("Blue");
  18.        
  19.         ArrayList<String> aListAllColors = new ArrayList<String>();
  20.         aListAllColors.add("White");
  21.         aListAllColors.add("Black");
  22.         aListAllColors.add("Cyan");
  23.        
  24.         /*
  25.          * To add all values from any Collection to ArrayList use,
  26.          * boolean addAll(Collection c)
  27.          * method of Java ArrayList class.
  28.          */
  29.        
  30.         //add all elements from one ArrayList to another
  31.         aListAllColors.addAll(aListBasicColors);
  32.        
  33.         System.out.println("Added all elements from ArrayList" + aListAllColors);
  34.        
  35.     }
  36. }
  37.  
  38. /*
  39. Output of above given Java ArrayList addAll example would be
  40. Added all elements from ArrayList[White, Black, Cyan, Red, Green, Blue]
  41. */
Your rating: None Average: 2 (2 votes)
Share this

Post new comment

10 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.