my dog learned polymorphism*
The moose likes Java in General and the fly likes Integer Array to List conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Integer Array to List conversion" Watch "Integer Array to List conversion" New topic
Author

Integer Array to List conversion

Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12
Hello All,

I was trying the list collection.
first i tried by using a string data type. it went smooth.


This code gives me a perfect answers. But when I try the same with Integer datatype it fails.

Fails as in it does not throw an exception, but I thing it stores the array object in the index 0 of the .
It does not make a List of elements as it did with its counter string datatype.

Why is that so ?


Keep Moving Forward
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Lists only accept Objects, and int is not an Object, so it can't go into the List. Because of this the Arrays.asList(T source ...) method misunderstands what you are sending it (it assumes you are using varargs to create a List with 1 Object in it, that Object being an int[]).

You need to use the Integer wrapper Object instead of the primitive int:


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Devendra Walanj wrote:Hello All,

I was trying the list collection.
first i tried by using a string data type. it went smooth.


This code gives me a perfect answers.


It will help a bit if you understand why this works -- the asList() method is a generic method that takes a var-arg. You have to remember that generics only works with objects, and that var-args is implemented under-the-covers as arrays. So, when you pass in an array of objects (which an array of string is), it will use each element of the array as a different parameter (or to be more correct, the array is simply passed in).

Devendra Walanj wrote:But when I try the same with Integer datatype it fails.

Fails as in it does not throw an exception, but I thing it stores the array object in the index 0 of the .
It does not make a List of elements as it did with its counter string datatype.

Why is that so ?


The problem with this second case is that you don't have an array of objects -- primative types are not objects. So, to satisfy the condition, Java will use the array as the object, and pass the whole array as a single parameter. In other words, the list that is returned has one element, and the one element is the array itself.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12
Steve Luke wrote:Lists only accept Objects, and int is not an Object, so it can't go into the List. Because of this the Arrays.asList(T source ...) method misunderstands what you are sending it (it assumes you are using varargs to create a List with 1 Object in it, that Object being an int[]).

You need to use the Integer wrapper Object instead of the primitive int:


Yes you are right. I did thought so but was casting it ..... which also was not right logically.

Thanks for the clearing the issue.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Integer Array to List conversion
 
Similar Threads
Unnecessary cast in List?
Proper way to remove and shift elements of an array
java.util.List method addAll(int,Collection)?
Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays
Need suggestion on implementation