• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boxing with Arrays.asList()

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following examples:



The compiler complains that that int[] and java.lang.Integer are not compatible. i.e.


It works fine if i change the List definition to remove the generic types.



- Shouldn't the compiler have auto-boxed the ints to Integer?
- How can i create a `List<Integer>` object from an array of ints using
Arrays.asList()?

Thanks
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ziggy,

It seems that boxing can be done from int -> Integer and not int[] -> Integer[]. Also the non-generic List you have shown is not a List of all the array contents, but the array itself. Try below code.



The variable I have used is non<underscore>genericList... Not sure why underscore not shows up some time
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John
 
reply
    Bookmark Topic Watch Topic
  • New Topic