• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically name arrays

 
KTSAIC Thomas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Strange one for you...

I have a table which will contain generic columns like so:

PARAM0_NAME
PARAM0_TYPE
PARAM0_VALUE
PARAM1_NAME
PARAM1_TYPE
PARAM1_VALUE

etc, etc...up to 9. What I would like to do is dynamically create the arrays for each of these columns at runtime, something like:

...
int i = 0;

while ( i < 10; i++ ){
String[] strParam<i>_name = new String[noReccount]
String[] strParam<i>_type = new String[noReccount]
String[] strParam<i>_value = new String[noReccount]
}
...

Any thoughts on whether this is feasible? I guess I just don't want to have to type it all out (I'm being lazy!)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a variable number of String[] that can be located by names made up on the fly, I suggest you look into the "collections" API in the java.util package. Specifically the various Map implementations.
Bill
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use Strings? Wouldn't it be better to make a small Parameter class with name, value, and type properties and create arrays of those?

(Warning - the following code hasn't been tested/compiled/anything - it's just out of my head, and is to serve as a general outline of the process...


I don't see anything specific to JSPs in this question... moving to Java Programming in General (Intermediate)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. You should create a class with fields that correspond to the columns in your table. Then you just have to manage a single array with instances of this class.

HTH

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic