Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays.asList with character array  RSS feed

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following throws an exception, but I don't know why. Can someone explain?

Is it a case of autoboxing not working on characters?



Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: java.util.ArrayList<java.lang.Character>
found: java.util.List<char[]>
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you get that Exception? Are you using code from an IDE which has already shown an error and you have told it to proceed regardless?

I think it means the asList() method won't accept primitive arrays as an argument.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the entire source code if possible and if its not too long. The argument type you pass to asList and the type of List must be the same. You passed char[] , an array. But the list is of a different type -Character. I tried to make a program out of your code (it compiled), here it is :



[EDIT : and also what wouter oet says below]
I dont know if this is what you intended to do.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that Arrays.asList() returns a List<T> and you're trying to assign it to an ArrayList.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would work if output would be a Character[]. But since the "T" generic type can only handle objects and not primitives it uses char[] for T.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How did you get that Exception? Are you using code from an IDE which has already shown an error and you have told it to proceed regardless?

Yep, I didn't believe it so I told it to run anyway.
I think it means the asList() method won't accept primitive arrays as an argument.

I think you're right, but it's OK with non-primitive arrays:

Rahul Sudip Bose wrote:I dont know if this is what you intended to do.
No, was trying to get a List of Characters, not char[]s.

Wouter Oet wrote:The problem is that Arrays.asList() returns a List<T> and you're trying to assign it to an ArrayList.
That was also a problem, but not the main one.

Rob Spoor wrote:It would work if output would be a Character[]. But since the "T" generic type can only handle objects and not primitives it uses char[] for T.

OK.

After some more googling it seems Java doesn't have a direct way of converting primitive arrays to Lists.

In terms of the original code I have to do this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!