• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically name arrays

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!)
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This will take every ounce of my mental strength! All for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic