Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer Array to List conversion

 
Devendra Walanj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Devendra Walanj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic