Monday, February 4, 2013

Copy Elements from one ArrayList to Another ArrayList

Here is the Program to copy the values from one ArrayList to another ArrayList.

?
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
package collect;
  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
  
  
public class ListClas {
 public static void main(String[] args) {
  
  List <STRING>l1=new ArrayList<STRING>(); 
    
  l1.add("hi");
  l1.add("brother");
    
  ArrayList<STRING> al=new ArrayList<STRING>(); 
  al.add("hello");  
  al.add("guru");   
    
  Collections.copy(al,l1);   
  Iterator i= al.iterator();  
    
  while(i.hasNext()){ 
   i.hasNext();  
   System.out.print(" "+i.next()); 
   //System.out.println(al);  
  }   
 }  
}

This is the procedure to copying the objects from one list to another.

No comments:

Post a Comment