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

Java ArrayList Copy

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. //Java ArrayList Copy
  5.  
  6. /*
  7.  * This Java ArrayList copy example shows how to copy one ArrayList
  8.  * to another using copy method of Collections class.
  9.  */
  10. public class JavaArrayListCopy {
  11.    
  12.     public static void main(String args[]){
  13.        
  14.         ArrayList<String> alistNumbers = new ArrayList<String>();
  15.         alistNumbers.add("1");
  16.         alistNumbers.add("2");
  17.         alistNumbers.add("3");
  18.        
  19.         ArrayList<String> alistNumbersInWords = new ArrayList<String>();
  20.         alistNumbersInWords.add("One");
  21.         alistNumbersInWords.add("Two");
  22.         alistNumbersInWords.add("Three");
  23.         alistNumbersInWords.add("Four");
  24.         alistNumbersInWords.add("Five");
  25.    
  26.         /*
  27.          * To copy one ArrayList to another, use
  28.          * static void copy(List destinationList, List sourceList)
  29.          * method of Collections class.
  30.          *
  31.          * copy method copies all elements of one ArrayList to another one and
  32.          * thus overwriting any value which is already present in the destination
  33.          * List.
  34.          *
  35.          */
  36.        
  37.         //all elements of ArrayList alistNumbers will be copied to alistNumbersInWords
  38.         Collections.copy(alistNumbersInWords, alistNumbers);
  39.        
  40.         System.out.println(alistNumbersInWords);
  41.        
  42.         /*
  43.          * Note: Make sure that destination list is long enough to hold all
  44.          * copied elements. If this is not the case, ArrayIndexOutOfBoundsException
  45.          * will be thrown.
  46.          */
  47.     }
  48. }
  49.  
  50. /*
  51. Output of above given Java ArrayList copy example would be
  52. [1, 2, 3, Four, Five]
  53. */
Your rating: None Average: 4 (1 vote)
Share this
Anonymous's picture

A small Correction, when

A small Correction, when destination list is not long enough to hold all copied elements it gives "java.lang.IndexOutOfBoundsException" not "java.lang.ArrayIndexOutOfBoundsException"
Anonymous's picture

This is one superb blog i

This is one superb blog i missed all these days. Thanks to Google for bringing up front. I have bookmarked and please let the logic be flowing. Driver Download
Anonymous's picture

I feel this is very useful. I

I feel this is very useful. I spend serveral time to analysis lot of defect. After i came to blog as soon am getting solutions. Thanks to Google for bringing up front. Thanks Periyasamy C
Anonymous's picture

Please visit my blog and give

Please visit my blog and give feedbacks [URL="http://yourjavatutor.blogspot.in/"]http://yourjavatutor.blogspot.in/[/URL]
Anonymous's picture

Please visit my blog and give

Please visit my blog and give feedbacks [URL="http://yourjavatutor.blogspot.in/"]http://yourjavatutor.blogspot.in/[/URL]

Post new comment

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