0

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?

5
  • why don't you use arrays or lists? Commented Mar 7, 2012 at 9:14
  • Why not use an array to hold each "set" and simply use the index? Commented Mar 7, 2012 at 9:15
  • @deporter: I will try lists. It seems to be a good Idea. Will HashMap also be appropriate? Commented Mar 7, 2012 at 9:23
  • @user1139023 no hashmap! use arraylist or array. Commented Mar 7, 2012 at 9:38
  • @user1139023 how will you populate the hashmap? manually? it is not a good practice for your problem, use list. Commented Mar 7, 2012 at 9:55

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

8 Comments

but did you get the question, he doesn't mention about his desire to use arrays or lists.he is asking a "varying portion" in the name of a variable. ok?
@deporter: I indeed got the question. But this is the correct answer; the OP needs to be informed that what he/she wants is not how Java works.
+1: This is what should be done, a data structure. Although the OP asked about pseudo-reflection, as @OliCharlesworth mentioned - dont. A HashMap could also work here too to meet the OPs requirements.
@Deco: Even I was thinking of HashMaps. Seems to be a good Idea. thank you . I will try that also.
@user1139023: There's little value in a HashMap here. HashMaps are useful when your indexes are non-contiguous, or are a complex data-type.
|
1

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

Comments

1

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)

2 Comments

Does using Map have any side affects - like consumer memory or something like that. Basically I am asking was hash map meant for this - accessing your object using a string to refer to it.
The overhead is minimal. The collection classes all have their distinct qualities and capabilities. You can use a Map anytime you wish to create a set of references from one domain to another. For example Map<Person, Address> workplaceAddresses or Map<Integer, Element> periodicTableOfElements etc.
0

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

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

Comments

0

create an arraylist instead and reference using the list index

1 Comment

he is asking a varying portion in the name of a variable. of course arrays should be preferred. but the question is different. i will downvote it, please remove.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.