• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with dinamic variable names  RSS feed

 
Hugo Alejandro
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I�m new in this forum and I hope you can help me.
I'm trying to make an ArrayList which contains Arrays; each Array will be a ResultSet from a SQL Server 2005 query, and I like that the Arrays name and content would be dynamic. I mean, the table form the DB is a list of products and many times, the product�s categories change so�

Is that possible? How would it be?
Thanks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages for an important administrative matter.

to answer your question, you can't. the reason you need to name something (like an array) is so that you can find it. if all your arrays are in your array list, you can find them by looking in your array list.

you may want to consider a map. This will let you put in pairs of data. for example, the first element of the pair could be a string, and the second an array. you could then query your map for the list of names and/or use a name to get the specific array you want.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that you don't have a computer science background.

Variable names cannot be dynamic in a computer language (*generally speaking) - the point of a variable is to name it for the compiler (or emulator). The code is not changed outside the visibility of the developer.


This is not always the case in scripting languages which can be very fast-and-loose on these things.


Now, the VALUE of the array items - certainly they can be changed/updated/replaced as often as you care to.

So, give it a try, show us your attempt, and ask us about some specifics of your problem.

FAQ.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Hugo Alejandro
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand. Now I have something different, I solve it by adding all the bidimensional arrays into an ArrayList but at the time I want to get them back, I can�t because the arrays I think were stored as Objects not as arrays, so how can I �convert� the Objects into bidimensional arrays again?

About the posting thing, I�m new but I promise won�t happen again
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try classcasting

. . .
Object obj = myList.get(0);
ResultSet[] results = (ResultSet[])obj;
. . .

but you are better using Java5 or Java6 and generics, then you declare your List like this

List<ResultSet[]> results;
. . .
results = new ArrayList<ResultSet[]>();

More details about generics in in the Java� Tutorials.
 
Hugo Alejandro
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!

I tried casting and it work:

for (int i = 0; i < arrayListName.size(); i++) {

String convertedArray[][]= (String [][])arrayListName.get(i);
....
}
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you take Fred's advise and use a Map instead. You can use the "variable names" as keys. The only problem is that the value type should be Object if you want different types, and you'll have to do a lot of checking (instanceof) and casting.

However, seeing your example uses String[], you could simply use a Map<String,String>.

HashMap is usually the best implementation choice if you don't need to preserve the sort order.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!