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