This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes more than one add in a list/set ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "more than one add in a list/set ?" Watch "more than one add in a list/set ?" New topic
Author

more than one add in a list/set ?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580

Is there a shortcut for that:



can I do something like?:

Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131


this is not possible.
other possible way could be ,put all your objects in an array and loop through it and
add it to the list


My Techie Blog - Java Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10733
    
    9

Check out the addAll() method.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20046
    
  30

But addAll requires that the objects are in another collection. Although using varargs, you could use the following:

I wouldn't do that however; although you save a few lines, it does create an extra array (for the varargs), and then another List object. In the end the result is the same. So you have to ask yourself this: is being lazy / shrinking your source code enough reason to warrant slower code (create the array and object takes time as well) that uses more memory?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks!
 
 
subject: more than one add in a list/set ?
 
It's not a secret anymore!