• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with declaring variables dynamically

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post the error that has occured.
 
GaganSinghSanto patiala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

>
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sent you another private message. Please check.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the help.

 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic