Thursday, 3 March 2016

ArrayList To Array conversion 1

import java.util.*;
class ArrayListToArray
{ public static void main(String[] args)
{ ArrayList al=new ArrayList();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
System.out.println("Contents of al : "+al);
System.out.println("Contents of al using iterator:");
Iterator itr = al.iterator();
while(itr.hasNext())
{
int x = (Integer)itr.next();
System.out.print(x + " ");
}
System.out.println();
Object ia[]= al.toArray();
int sum=0;
for(int i=0; i < ia.length; i++)
sum = sum + (Integer)ia[i];
System.out.println("Sum is "+sum);
}
}

No comments:

Post a Comment