Conversion Object[] to int[] error

user1079425 picture user1079425 · Jun 23, 2012 · Viewed 11.5k times · Source

Possible Duplicate:
How to convert List<Integer> to int[] in Java?

I have an ArrayList and when I try to convert it into an Array of Integer because I need to do operations which concerns integer, I get this error :

incompatible types
required: int[]
found: java.lang.Object[]

Here's my code :

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

// I execute an SQL statement, then :
while (resultat.next()) {
    ids_rdv.add(resultat.getInt("id_rdv"));
}
int[] element_rdv_id = ids_rdv.toArray(); //Problem

Do you have any idea about that?

Answer

user278064 picture user278064 · Jun 23, 2012

Assuming your original objects were instances of the Integer class:

Integer[] element_rdv_id = ids_rdv.toArray(new Integer[0]);