473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic array creation

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!
Jun 2 '06 #1
1 20520

"None" <no**@none.com> wrote in message
news:3b********************************@4ax.com...
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!


Use the Array.newInstance(Class, int) method:

http://java.sun.com/j2se/1.5.0/docs/...g.Class,%20int)

(note that there's a close parenthesis at the end that's part of this URL).

- Oliver

Jun 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dickseacup | last post by:
Is there a way to generate a dynamic image, passing an array (of arrays) to the image creation script, such that it displays along with text on a php generated HTML page? I either get header...
3
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu...
0
by: DotNetJunkies User | last post by:
Hie, I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
2
by: charliewest | last post by:
I need to create textboxes in real-time, the actual number of which is determine by a result from a database query. I have been able to create the controls, and then add them to the ASPX page....
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
1
by: dav3 | last post by:
Any help here is appreciated folks. First in my Person class the comments = errors visual basics is giving me and I am not sure why. Also when i try and set up my array of pointers to Student...
6
by: Cleber Schönhofen | last post by:
Hi, I need a dynamic array of objects, but don´t work. its work : DadosDoTime tt = new DadosDoTime; int i = 0; while (rd.Read()) { tt = new DadosDoTime(); tt.pos = 0;
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.