Conversion Object[] to int[] error [duplicate]
-
16-06-2021 - |
Question
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]);
OTHER TIPS
incompatible types required: int[] found: java.lang.Object[]
when you do ids_rdv.toArray()
, it returns an array of Objects, that can't be assigned to array of primitive integer types.
You need to get an array of Integer objects in hand, so write it this way
Integer[] element_rdv_id = ids_rdv.toArray(new Integer[]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow