Collections

Copy Elements of One Java ArrayList to Another Java ArrayList Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

4 Comments

  • 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!

    [code]
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.lang.Package;
    import java.util.*;

    public class ReadIntsArrayList
    {
    public static void main( String [ ] args )
    {
    ArrayList array = getStrings( );
    // ArrayList <string> duplicateValues = new ArrayList<string>( );
    // String token = null;
    int [ ] array2 = new int [ array.size( ) ];

    for( int i = 0; i < array.size( ); i++ )
    for (int j = i+1 ; j < array.size ( ) ; j++ )
    {
    if(array.get(j).equals(array.get(i)))
    {

    // array2[j] = array.get(j);
    // System.out.println(array2[j]);
    // duplicateValues.add(array.get(j));
    System.out.println(“i value is: “+array.get(i));
    System.out.println(“i index is: “+i);
    System.out.println(“j value is: “+array.get(j));
    System.out.println(“j index is: “+j);

    // System.out.println(array2[i]);

    }
    /*
    else
    {
    System.out.println(“The file contains no duplicates.”);
    }
    */
    }

    }

    public static ArrayList getStrings( )
    {
    FileReader theFile;
    BufferedReader fileIn = null;

    ArrayList <string> resList = new ArrayList<string>( );
    String oneLine;
    StringTokenizer tokenizer = null;
    // String token = null;

    try
    {
    theFile = new FileReader( “C:\partTwo1.txt” );
    fileIn = new BufferedReader( theFile );
    while( ( oneLine = fileIn.readLine( ) ) != null &&
    !oneLine.equals( “” ) )
    {
    tokenizer = new StringTokenizer(oneLine);

    while (tokenizer.hasMoreTokens()){
    resList.add(tokenizer.nextToken()); }
    }

    }
    catch( IOException e )
    {
    System.out.println( “Unexpected IO Exception has shortened amount read” );
    }

    // System.out.println( “Done reading” );
    return resList;
    }
    }
    [/code]

  • // After copy index of the elements in both source and destination lists would be identical.

    That is not true. The source list would be not changed.

  • //After copy index of the elements in both source and destination lists would be identical.

    After source list stays unchanged.

  • most suitable answer here:

    import java.util.ArrayList;

    import java.util.Iterator;

    import java.util.List;

    public class A {

    public static void main(String[] args) {

    List<string> l=new ArrayList<string>();

    l.add(“Ram”);

    l.add(“Shyam”);

    List<string> l2=new ArrayList<string>();

    l2.add(“Geeta”);

    System.out.println(l2.addAll(l));

    Iterator<string> l4=l2.iterator();

    while(l4.hasNext()){

    String s= l4.next();

    System.out.println(s);

    }

    }

    }

    output:

    true

    Geeta

    Ram

    Shyam

Sponsors

Facebook Fans