Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 23928
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.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic