+ 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);
5 Respostas
+ 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 :)
+ 4
Ah. the int array must be of type Integer.
+ 2
I don't see one just by looking at it. You could copy and paste it into a compiler and see for yourself. 😜
+ 2
yeah that was it 😃 thanks alot 😍
+ 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