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