+ 2

Is there an error with this code ?? The list.toArray() part

List <Integer> list = new ArrayList <Integer> (); Scanner input = new Scanner(System.in); while(input.hasNextInt()) list.add(input.nextInt()); int arr[] = new int[list.size()]; arr = list.toArray(arr);

16th May 2017, 2:21 PM
Nouran Samy
Nouran Samy - avatar
5 Answers
+ 14
Make the following change: Integer arr[] = new Integer[list.size()]; Because, toArray() is a generic method and it can take array of non-primitive type. (Just like you can't declare an ArrayList of type int). int is primitive and Integer is non-primitive, so it should work this way :)
16th May 2017, 3:11 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
Ah. the int array must be of type Integer.
16th May 2017, 3:06 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
I don't see one just by looking at it. You could copy and paste it into a compiler and see for yourself. 😜
16th May 2017, 2:37 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
yeah that was it 😃 thanks alot 😍
16th May 2017, 3:11 PM
Nouran Samy
Nouran Samy - avatar
+ 1
i compiled it and got an error i didn't understand about the syntax i guess but i searched about it and got nothing
16th May 2017, 2:55 PM
Nouran Samy
Nouran Samy - avatar