File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Spring in Action this week in the Spring 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

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

<a href="" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="" target="_blank" rel="nofollow">Java Questions</a>
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451

Check out the addAll() method.

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

Joined: Oct 27, 2005
Posts: 19718

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?

How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks!
I agree. Here's the link:
subject: more than one add in a list/set ?