Copy Elements of One Java ArrayList to Another Java ArrayList Example

  1. /*
  2.   Copy Elements of One Java ArrayList to Another Java ArrayList Example
  3.   This java example shows how to copy all elements of one Java ArrayList object to
  4.   another Java ArrayList object using copy method of Collections class.
  5. */
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9.  
  10. public class CopyElementsOfArrayListToArrayListExample {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. //create first ArrayList object
  15. ArrayList arrayList1 = new ArrayList();
  16.  
  17. //Add elements to ArrayList
  18. arrayList1.add("1");
  19. arrayList1.add("2");
  20. arrayList1.add("3");
  21.  
  22. //create another ArrayList object
  23. ArrayList arrayList2 = new ArrayList();
  24.  
  25. //Add elements to Arraylist
  26. arrayList2.add("One");
  27. arrayList2.add("Two");
  28. arrayList2.add("Three");
  29. arrayList2.add("Four");
  30. arrayList2.add("Five");
  31.  
  32. /*
  33.   To copy elements of one Java ArrayList to another use,
  34.   static void copy(List dstList, List sourceList) method of Collections class.
  35.  
  36.   This method copies all elements of source list to destination list. After copy
  37.   index of the elements in both source and destination lists would be identical.
  38.  
  39.   The destination list must be long enough to hold all copied elements. If it is
  40.   longer than that, the rest of the destination list's elments would remain
  41.   unaffected.
  42.   */
  43.  
  44. System.out.println("Before copy, Second ArrayList Contains : " + arrayList2);
  45.  
  46. //copy all elements of ArrayList to another ArrayList using copy
  47. //method of Collections class
  48. Collections.copy(arrayList2,arrayList1);
  49.  
  50. /*
  51.   Please note that, If destination ArrayList object is not long
  52.   enough to hold all elements of source ArrayList,
  53.   it throws IndexOutOfBoundsException.
  54.   */
  55.  
  56. System.out.println("After copy, Second ArrayList Contains : " + arrayList2);
  57. }
  58. }
  59.  
  60. /*
  61. Output would be
  62. Before copy, Second ArrayList Contains : [One, Two, Three, Four, Five]
  63. After copy, Second ArrayList Contains : [1, 2, 3, Four, Five]
  64. */

Need help with code

Hi, I am trying to print out all the duplicates in a file and seem to be gettting multiple instances of the same duplicate values. Can you take a look at my code and see what I am doing worng? Thanks!

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.FileReader;
  4. import java.util.ArrayList;
  5. import java.lang.Package;
  6. import java.util.*;
  7.  
  8.  
  9. public class ReadIntsArrayList
  10. {
  11. public static void main( String [ ] args )
  12. {
  13. ArrayList array = getStrings( );
  14. // ArrayList <String> duplicateValues = new ArrayList<String>( );
  15. // String token = null;
  16. int [ ] array2 = new int [ array.size( ) ];
  17.  
  18.  
  19.  
  20. for( int i = 0; i < array.size( ); i++ )
  21. for (int j = i+1 ; j < array.size ( ) ; j++ )
  22. {
  23. if(array.get(j).equals(array.get(i)))
  24. {
  25.  
  26. // array2[j] = array.get(j);
  27. // System.out.println(array2[j]);
  28. // duplicateValues.add(array.get(j));
  29. System.out.println("i value is: "+array.get(i));
  30. System.out.println("i index is: "+i);
  31. System.out.println("j value is: "+array.get(j));
  32. System.out.println("j index is: "+j);
  33.  
  34. // System.out.println(array2[i]);
  35.  
  36. }
  37. /*
  38.   else
  39.   {
  40.   System.out.println("The file contains no duplicates.");
  41.   }
  42.   */
  43. }
  44.  
  45.  
  46. }
  47.  
  48.  
  49. public static ArrayList getStrings( )
  50. {
  51. FileReader theFile;
  52. BufferedReader fileIn = null;
  53.  
  54. ArrayList <String> resList = new ArrayList<String>( );
  55. String oneLine;
  56. StringTokenizer tokenizer = null;
  57. // String token = null;
  58.  
  59. try
  60. {
  61. theFile = new FileReader( "C:\\partTwo1.txt" );
  62. fileIn = new BufferedReader( theFile );
  63. while( ( oneLine = fileIn.readLine( ) ) != null &&
  64. !oneLine.equals( "" ) )
  65. {
  66. tokenizer = new StringTokenizer(oneLine);
  67.  
  68. while (tokenizer.hasMoreTokens()){
  69. resList.add(tokenizer.nextToken()); }
  70. }
  71.  
  72. }
  73. catch( IOException e )
  74. {
  75. System.out.println( "Unexpected IO Exception has shortened amount read" );
  76. }
  77.  
  78. // System.out.println( "Done reading" );
  79. return resList;
  80. }
  81. }

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!