Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- import java.util.ArrayList;
- import java.util.Collections;
- //Java ArrayList Copy
- /*
- * This Java ArrayList copy example shows how to copy one ArrayList
- * to another using copy method of Collections class.
- */
- public class JavaArrayListCopy {
- public static void main(String args[]){
- ArrayList<String> alistNumbers = new ArrayList<String>();
- alistNumbers.add("1");
- alistNumbers.add("2");
- alistNumbers.add("3");
- ArrayList<String> alistNumbersInWords = new ArrayList<String>();
- alistNumbersInWords.add("One");
- alistNumbersInWords.add("Two");
- alistNumbersInWords.add("Three");
- alistNumbersInWords.add("Four");
- alistNumbersInWords.add("Five");
- /*
- * To copy one ArrayList to another, use
- * static void copy(List destinationList, List sourceList)
- * method of Collections class.
- *
- * copy method copies all elements of one ArrayList to another one and
- * thus overwriting any value which is already present in the destination
- * List.
- *
- */
- //all elements of ArrayList alistNumbers will be copied to alistNumbersInWords
- Collections.copy(alistNumbersInWords, alistNumbers);
- System.out.println(alistNumbersInWords);
- /*
- * Note: Make sure that destination list is long enough to hold all
- * copied elements. If this is not the case, ArrayIndexOutOfBoundsException
- * will be thrown.
- */
- }
- }
- /*
- Output of above given Java ArrayList copy example would be
- [1, 2, 3, Four, Five]
- */

A small Correction, when
This is one superb blog i
I feel this is very useful. I
Please visit my blog and give
Please visit my blog and give
Post new comment