Search code examples
javaarraylisttype-conversiontoarray

Conversion Object[] to int[] error


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?


Solution

  • Assuming your original objects were instances of the Integer class:

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