How to cast ArrayList to ....

Hi,

I got an ArrayList and its values are (1,2,3)

List tempList = new ArrayList();

tempList.add("1");

tempList.add("2");

tempList.add("3");

I am wondering how to convert it to an int [] array.

int [] tempInt;

I know that I can do this : tempList.toArray

but I don't know how to cast this to int[] type of array

(note: not integet [] , just int[])

Can anyone help? thanks a lot.

[480 byte] By [newtojava99] at [2007-9-26 2:59:16]
# 1

Here we go....

int[] in=new int[3];

int count=0;

Iterator it=tempList.iterator();

while(it.hasNext())

{

in[count]=Integer.ParseInt(it.next()+"");

count++;

}

cheers....

sasivarnan at 2007-6-29 10:55:08 > top of Java-index,Archived Forums,Java Programming...
# 2

Note: It would be much more efficient to use the wrapper classes for storing primitives in a Vector/ArrayList/LinkedList/... :

tempList.add(new Integer(1));

tempList.add(new Integer(2));

tempList.add(new Integer(3));

(getting the array as an int[] still requires a for-loop)

jsalonen at 2007-6-29 10:55:08 > top of Java-index,Archived Forums,Java Programming...