Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Integer Array to List conversion

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
money grubbing section goes here:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic