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 / June 2006

Tip: Looking for answers? Try searching our database.

Dynamic array creation

Thread view: 
None - 02 Jun 2006 03:07 GMT
Dynamic array creation

Hi all... here's a good one for you...

I have a situation where I have some bean, and I need to populate an
array field in it... here's the problem... I do not know the element
type of the array before runtime.

Now, I've been planning on using Commons Beanutils, since the rest of
this particular app does (it's doing a lot of introspection of the
beans and such).

So, here's the question... can anyone figure out a way to dynamically
create an array at runtime?  What I mean is, I want to do the
followig:

PropertyUtils.setProperty(obj, fieldName,
((List)fieldValues).toArray());

So, I want to set the field named by fieldName on the bean instance
referenced by obj, and I want to do it by taking the fieldValues List
and converting it to an array.  Now, I can do:

PropertyUtils.setProperty(obj, fieldName,
((List)fieldValues).toArray(new String[0]));

...and that's great, except that I don't know until runtime that the
field of the bean is of type String, it could be anything else.  So,
how can I dynamically do the equivalent of the new String[0] is really
the question?

FYI, I don't really care if this is done with Beanutils, but I think
that's probably the logical course of action.  Any ideas?  Thanks all!
Stefan Ram - 02 Jun 2006 03:20 GMT
>how can I dynamically do the equivalent of the new String[0] is
>really the question?

 java.lang.reflect.Array.newInstance( myClass, 0 )
Oliver Wong - 02 Jun 2006 15:39 GMT
> Dynamic array creation

   Multipost, don't crosspost:
http://www.cs.tut.fi/~jkorpela/usenet/xpost.html

   I've answer your question in another newsgroup.

   - Oliver


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.