• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with declaring variables dynamically

 
GaganSinghSanto patiala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post the error that has occured.
 
GaganSinghSanto patiala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11073
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ringer smack wrote:

Please check your private messages for an important administrative matter
 
GaganSinghSanto patiala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11073
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sent you another private message. Please check.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
GaganSinghSanto patiala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic