| Author |
can we add primitive variables in ArrayList or just objects?
|
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 973
|
|
|
can we add primitive variables in arraylist or just objects
|
http://www.lifesbizzare.blogspot.com || OCJP:81%
|
 |
Chinna Eranna
Ranch Hand
Joined: Dec 08, 2009
Posts: 174
|
|
You can add *only* objects.
You can use.. wrapper classes.. for store the primitive data.
|
- Chinna
|
 |
Karthick Dharani Vidhya
Ranch Hand
Joined: Feb 23, 2008
Posts: 88
|
posted

0
|
With the AUTO BOXING feature, the below code will work fine
But even though you add primitive ones here, Auto boxing is done and all these values are added as Integer objects
|
Karthick D V
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 973
|
|
Karthick Dharani Vidhya wrote:With the AUTO BOXING feature, the below code will work fine
But even though you add primitive ones here, Auto boxing is done and all these values are added as Integer objects
why aint you used
|
 |
Karthick Dharani Vidhya
Ranch Hand
Joined: Feb 23, 2008
Posts: 88
|
posted

0
|
Should have used it. Thanks.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Vishal Hegde wrote:why aint you used
Surely you mean this?
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 973
|
|
Ritchie sir , arent both correct?
i mean
ArrayList<Integer>a=new ArrayList<Integer>(); //as arraylist is a class
List<Integer>iList=new ArrayList<Integer>(); //list is an interface
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1611
|
|
Vishal Hegde wrote:Ritchie sir , arent both correct?
i mean
ArrayList<Integer>a=new ArrayList<Integer>(); //as arraylist is a class
List<Integer>iList=new ArrayList<Integer>(); //list is an interface
Campbell's solution is best. It is my understanding that you should always code to an interface (when possible) and not its implementation.
I think this allows for greater flexibility if you need to change something down the line.
|
When you do things right, people won't be sure you've done anything at all.
|
 |
 |
|
|
subject: can we add primitive variables in ArrayList or just objects?
|
|
|