Pull Nointer
Pull Nointer

Reputation: 33

Declaring many variables in a loop

so here's my example code of what I'm trying to do (it doesn't compile):

    for(char name = 'a'; name <= 'z'; name++)
    {
        double <<name>>;
    }

In this example I want to create 26 different variables, without explicitly listing

double a, b, c, d, e; //and so on

How can I go about doing that? Thanks!

Upvotes: 0

Views: 112

Answers (2)

Hardip Patel
Hardip Patel

Reputation: 85

If you want to retrieve your values like variable may be you can use HashMap!

HashMap<Character,Double> variables = new HashMap<Character,Double>();
for(char name = 'a'; name <= 'z'; name++)
    {
        variables.put(name,"put double value here");
    }

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

Use an array:

double[] vars = new double[26];
int i = 0;
for(char name = 'a'; name <= 'z'; name++)
{
    vars[i++] = name;
}

Upvotes: 2

Related Questions