• 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

Arrays.asList cannot be used with arrays of primitives?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


^ won't work but this one below will?



Okay. What would be the equivalent for a string array? In this example? "Integer" is just a word? I mean shouldn't it be declared first someone else, like a type of some kind? So we could use



As long as "Taco" is typed somewhere I'm guessing?

 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK so some more searching led me to something about a "wrapper" and the fact that String is in fact already a wrapper class. So why won't this work? I guess it's got nothing to do with primitives? My bad.

Edit:

The solution was I needed to include the correct libraries such as

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;

leaving this up for other noobs like me, but it's been solved. Include the correct libraries and use a correct wrapper class.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the original question, the one about using Arrays.asList() on a primitive array int[]:

Unfortunately, I don't think that does what you think it does. As an experiment, try to get the first element of the List you've created with Arrays.asList(), and print it out. What is it? Is it what you expected? I think it will not be. This is an unfortunate side effect of having too many overloads.

For comparison, Google's Guava library offers Ints.asList(int...), which I think does exactly what you want. Google does a good job of finding and plugging holes like this in the APIs.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the documentation for Arrays#asList(T...). The writers of the documentation expect you to know that “T” is a generic type and generics only applies to reference types, not primitives. You will have no end of difficulty getting your Taco[] array past the compiler; it expects you to know that boxing conversions only apply to the 8 wrapper types and the 8 primitives. Note there is un-boxing as well; you can box any primitive type and unbox any wrapper object pointed to by a reference, but not null.

There are several different meanings for the term “wrapper class”; I think you are using it here to mean one of the 8 classes which correspond to the 8 primitive types. You can’t call String a wrapper class, then. It doesn’lt “wrap” anything. I don’t think String ever falls within any of the meanings of wrapper class.
 
reply
    Bookmark Topic Watch Topic
  • New Topic