Conversion Object[] to int[] error [duplicate]
Asked Answered
N

2

9

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?

Naze answered 23/6, 2012 at 17:55 Comment(0)
W
9

Assuming your original objects were instances of the Integer class:

Integer[] element_rdv_id = ids_rdv.toArray(new Integer[0]);
Wound answered 23/6, 2012 at 17:58 Comment(4)
Thank you !! It works now !! but I still don't understand what did you do ?Naze
Simply Integer isn't the same data type as int. Also if you later need any value from this Integer[] array you may need to use element_rdv_id[i].intValue() instead of element_rdv_id[i] as you would expect.Primal
thank you @Primal :) what about new Integer[0] ?Naze
Have a look herePrimal
M
1
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[]);
Maddiemadding answered 23/6, 2012 at 18:14 Comment(1)
Thank you Ahmad, I understand now, answer upvoted ;)Naze

© 2022 - 2024 — McMap. All rights reserved.