Ashwin
Ashwin

Reputation: 13547

Referring a variable with a variable in java

I have got into a strange strange situation. I have 3 sets of strings like this

String set1q1="something";    //associated with randomNum=1
String set1q2="something";
String set1q3="something";
String set1q4="something";
... and so on

String set2q1="something";    //randomNum=2
String set2q2="something";
String set2q3="something";
String set2q4="something";
... and so on

String set3q1="something";    //randomNum=3
String set3q2="something";
String set3q3="something";
String set3q4="something";
... and so on

All these strings are initialised only once. Now in my program i generate a random number between 1-3. I converted this random number into a string and stored it into a string called set.

String set=randomNum.toString();

Now next intead of using "if-else" to send the data(if randomnum=1 send set1q1-5, if randomnum=2 then send set2q1-5), I want the appropriate data to be sent using one line.

For example: if random no 2 is chosen then set2q1 has to be sent where the "2" in between is has to be the value of "set"(which is defined above).

set"set"q1    //where set can be 1,2,3

Is there any way to do this?

Upvotes: 0

Views: 134

Answers (5)

Synesso
Synesso

Reputation: 38988

Sounds like you want to index Strings by two indices. You should use a two-dimensional String array: String[][] strings. You may then access the desired string with strings[n][m]

Or you can achieve the same effect with a List<List<String>> strings if you need the dimensions of your 2D array to grow dynamically. You'd access the value you need with strings.get(n).get(m)

If you really want to access your strings by a composed name such as set2q1, then you just need a Map<String, String> strings. Then you'd access each value with strings.get("set" + m + "q" + n)

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272677

What you are asking for is not possible;1 it's just not the way Java works. Why don't you just use an array, or a collection?

List<List<String>> allTheStrings = new ArrayList<List<String>>();

List<String> myStrings = null;

// Add a subset
myStrings = new ArrayList<String>();
myStrings.add("something");
myStrings.add("something");
myStrings.add("something");
allTheStrings.add(myStrings);

// Add another subset
myStrings = new ArrayList<String>();
myStrings.add("something");
myStrings.add("something");
myStrings.add("something");
allTheStrings.add(myStrings);


...

// Obtain one of the strings
String str = allTheStrings.get(1).get(2);

1. Except in the case where these variables are members of a class, in which case you could use reflection. But really, don't.

Upvotes: 6

bisgardo
bisgardo

Reputation: 4610

It is not possible. Local variable identifiers are converted to numbers (stack offsets) during compilation. But you should use arrays or collections anyway

Upvotes: 1

NimChimpsky
NimChimpsky

Reputation: 47290

create an arraylist instead and reference using the list index

Upvotes: 0

Tom
Tom

Reputation: 4180

looks to me you should look into arrays, like this:

String[] strings = new String[]{"xxx", "yyy", "zzz"};
String string = strings[randomNumber];

Upvotes: 0

Related Questions