Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

ArrayList to Varargs

Thread view: 
moop™ - 12 Dec 2005 08:14 GMT
Hi,
I use a arraylist to store parameters for a perticular method, which is
going to be invoked via reflection. The method maybe defined in any
number of parameters, or be defined in varargs, which I cannot ensure
at design time. How can I use my parameter arraylist to invoke the
methods? Thank you!
ricky.clarkson@gmail.com - 12 Dec 2005 13:48 GMT
Reflection is usually a waste of effort, you'd be better off using
normal Java or going the whole hog to a dynamically-typed language.
Reflection loses static type safety, and doesn't gain the apparent
advantages of dynamic languages either.

Anyway,

You can see whether a Method is var args, using the rather cryptic
method:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html#isVarArgs()

So if (that's true) method.invoke(new Object[]{arrayList.toArray()});
else method.invoke(arrayList.toArray());


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.