It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Arrays.asList cannot be used with arrays of primitives? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays.asList cannot be used with arrays of primitives?" Watch "Arrays.asList cannot be used with arrays of primitives?" New topic
Author

Arrays.asList cannot be used with arrays of primitives?

Dick Hammer
Greenhorn

Joined: Jan 28, 2012
Posts: 18


^ 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

Joined: Jan 28, 2012
Posts: 18
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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37976
    
  22
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.
 
wood burning stoves
 
subject: Arrays.asList cannot be used with arrays of primitives?
 
Similar Threads
boxing,unboxing,for each and varargs
Behavior of Arrays.asList()
Question of Arrays.asList()
Random Numbers
interesting question.. !!