| Author |
Problem with declaring variables dynamically
|
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Posts: 4
|
|
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
Posts: 110
|
|
|
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
Posts: 4
|
|
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
Rancher
Joined: May 21, 2006
Posts: 414
|
|
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
Saloon Keeper
Joined: Jul 26, 2007
Posts: 7059
|
|
ringer smack wrote:
Please check your private messages for an important administrative matter
|
[Donate a pint, save a life!] [How to ask questions]
|
 |
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Posts: 4
|
|
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
Saloon Keeper
Joined: Jul 26, 2007
Posts: 7059
|
|
|
Sent you another private message. Please check.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 11225
|
|
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 7 API documentation
Scala Notes - My blog about Scala
|
 |
GaganSinghSanto patiala
Greenhorn
Joined: Feb 08, 2010
Posts: 4
|
|
Thanks a lot for the help.
|
 |
 |
|
|
subject: Problem with declaring variables dynamically
|
|
|