473,657 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.s etProperty(obj, fieldName,
((List)fieldVal ues).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.s etProperty(obj, fieldName,
((List)fieldVal ues).toArray(ne w 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 20547

"None" <no**@none.co m> wrote in message
news:3b******** *************** *********@4ax.c om...
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.s etProperty(obj, fieldName,
((List)fieldVal ues).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.s etProperty(obj, fieldName,
((List)fieldVal ues).toArray(ne w 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.newInstan ce(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
2523
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 errors (can't modify headers already sent), or I get the php script passing 'Array' to the image script instead of the actual array variable/object. How do I get dynamic, incline images to show up with surrounding text?
3
4771
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 creation takes some time. The approach I took was to override the OnSelect method of the MenuItem class, which had code to populate the
0
1664
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 i change the text displayed in the label in the same row. hier is my code: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web;
11
4724
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 create content to put at the address pointed to by f. In particular, I'd like to avoid/replace the memcpy line. Possible application (inspired by Paul Graham, "ANSI Common Lisp",
2
16239
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. However, when i submit the page, i am unable to read the values entered in these new textboxes. I have first tried to use System.Web.UI.WebControls.TextBox namespace, however, i was unable to read any value from my textboxes using the following...
13
4707
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 create an array (using "new") since one doesn't know it size when writing the program. However, it looks to me that the size information must come from somewhere at run time, and this information can be passed to array creation function as a...
1
1584
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 class I get the error that is in the comment. This is really bothering me as I spent the last hour and a half with a classmate working on this and we can not figure out whats up. class Person { public: Person(int sinNumber, char studentName);...
6
197
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
3387
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. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it vary in color range. Now when i used that php script it generates image successfully but it pixel ate...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.