Tuesday, 15 October 2013

java program to copy values from a arraylist to array.

Here is the source code ...


       

import java.util.*;
class Arraylist_to_array {
 public static void main(String[] a) {
  ArrayList k = new ArrayList();
  k.add(new Integer(1));
  k.add(new Integer(2));
  k.add(new Integer(3));
  k.add(new Integer(4));
  k.add(new Integer(5));
  k.add(new Integer(4));
  System.out.println("contents of ArrayList=" + k);

  Object arr[] = new Object[6];
  arr = k.toArray();
  int sum = 0, i;
  for (i = 0; i < arr.length; i++) {
   sum += ((Integer) arr[i]).intValue();
  }
  System.out.println("sum of the contents of the array=" + sum);
 }
}

       
 

No comments:

Post a Comment