converting ArrayList to int[]

hey, I am stuck with this : how to convert arrayList to int[] ?

got this so far

ArrayList array;

here i add objects to array;

int [] tab = new int[array.size()];

then I use :

array.toArray(tab);

but it simply does not work, can anybody help me out ?

cheers, Lukasz

[319 byte] By [Koniecznya] at [2007-11-26 15:03:05]
# 1

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class XXXX

{

public static void main(String[] args)

{

List<Integer> list = new ArrayList<Integer>();

// ArrayList only contains objects, can't hold int

list.add(1); // use autoboxing to add an Integer

list.add(2);

Integer[] ar = list.toArray(new Integer[list.size()]);

// create an int array from an Integer array

int[] intArray = new int[ar.length];

for(int i =0; i<ar.length; ++i)

{

intArray[i] = ar[i];

}

System.out.println(Arrays.toString(intArray));

}

}

>

ChuckBinga at 2007-7-8 8:52:21 > top of Java-index,Core,Core APIs...
# 2

So what is the intention of method Object[] toArray(), if it is not using reflection and by using this method I cant explicitly cast to my class and I am loosing the information about my destination class, and why method Object[] toArray(Object[]) allows it. It is the question to the author of this method, still I cant buy this method toArray(). Please help!

If there is a reasonable intention about this method, should it be included in JavaDoc.

Message was edited by:

dom

sdda at 2007-7-8 8:52:21 > top of Java-index,Core,Core APIs...
# 3
int [] tab = array.toArray(new int[0]);If it doesn't work, I made a typo.OleVV
OleVVa at 2007-7-8 8:52:21 > top of Java-index,Core,Core APIs...
# 4
Sorry, in Java 1.4 you have to make an explicit cast, so it'sint [] tab = (int[]) array.toArray(new int[0]);
OleVVa at 2007-7-8 8:52:21 > top of Java-index,Core,Core APIs...
# 5

> Sorry, in Java 1.4 you have to make an explicit cast,

> so it's

>

> int [] tab = (int[]) array.toArray(new int[0]);

This doesn't work because List's can only hold Objects, and the toArray() method can only work with an Object[], but not an array of primitives.

The only way to do it is the old fashioned way, loop thru the list and populate the array.

~Tim

SomeoneElsea at 2007-7-8 8:52:21 > top of Java-index,Core,Core APIs...
# 6

> This doesn't work because List's can only hold

> Objects, and the toArray() method can only work with

> an Object[], but not an array of primitives.

>

> The only way to do it is the old fashioned way, loop

> thru the list and populate the array.

>

> ~Tim

Like this, ChuckBing had the right Idea, but too much code

public static void main(String[] args) {

List<Integer> array = new ArrayList<Integer>();

array.add(1);

array.add(2);

array.add(3);

int[] tab = new int[array.size()];

for(int i = 0; i<array.size(); i++)

{

tab[i] = array.get(i);

}

System.out.println(Arrays.toString(tab));

}

~Tim>

SomeoneElsea at 2007-7-8 8:52:21 > top of Java-index,Core,Core APIs...