Author
Problem with declaring variables dynamically
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Messages: 4
posted Feb 08, 2010 22:41:09
Hi,
Needed help with declaring variables dynamically,
String[] a = {"Cheese", "Pepperoni", "Black Olives"};
for(int arg=0;arg<a.length;arg++)
{
HashMap ------- =new HashMap ();
}
I wanted the hashmap variable names to be Cheese Pappereni ..etc.
Thanks.
PrasannaKumar Sathiyanantham
Ranch Hand
Joined: Nov 12, 2009
Messages: 96
posted Feb 08, 2010 22:44:46
can you post the error that has occured.
To err is human,
To forgive is not company policy
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Messages: 4
posted Feb 08, 2010 22:53:05
Thanks for the reply prassana.
Actually i dint had a code in place...just wanted to see if there is a way to do that thing.
Basically i want to name my variables according to values in the array,so that i use the a variable for a set of operations.
Hope i dint confuse you.
Thanks.
Pushkar Choudhary
Ranch Hand
Joined: May 21, 2006
Messages: 352
posted Feb 08, 2010 22:53:13
ringer smack wrote:
Hi,
Needed help with declaring variables dynamically,
String[] a = {"Cheese", "Pepperoni", "Black Olives"};
for(int arg=0;arg<a.length;arg++)
{
HashMap ------- =new
HashMap ();
}
I wanted the hashmap variable names to be Cheese Pappereni ..etc.
Thanks.
Welcome to Javaranch!
Please use code tags when you post any code so that it looks neat like this:
>
Maneesh Godbole
Bartender
Joined: Jul 26, 2007
Messages: 4241
posted Feb 08, 2010 22:54:48
ringer smack wrote:
Please check your private messages for an important administrative matter
work is the scourge of the drinking class
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Messages: 4
posted Feb 08, 2010 23:02:43
Sorry , i missed to tag the code
Hi,
Needed help with declaring variables dynamically,
I wanted the hashmap variable names to be Cheese Pappereni ..etc.
Maneesh Godbole
Bartender
Joined: Jul 26, 2007
Messages: 4241
posted Feb 08, 2010 23:16:56
Sent you another private message. Please check.
work is the scourge of the drinking class
Jesper Young
Java Cowboy
Bartender
Joined: Aug 16, 2005
Messages: 7852
posted Feb 09, 2010 01:31:43
This is not the first time someone asks a similar question. You cannot declare variable names dynamically, that's not how Java works. Why do you want to do this?
Use a different kind of data structure. You could use a Map, for example, to store objects by name.
Java Beginners FAQ - JavaRanch SCJP FAQ
The Java Tutorial - Java SE 6.0 API documentation
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Messages: 4
posted Feb 09, 2010 02:20:25
Thanks a lot for the help.