0

I would like to ask how can I get the value of predefined value to its name

The following is my code

public class Calculation_Activity extends Activity{

int a=1;
int b=2;
int c=50;
int result;
String array1[]=new String[]{"a","b","c"};

}

I would like to ask how can I get the value of the string by using array1[i]?

for instance, I would like to use array1[3]to call the value of c[ie.50]

May you give me some advice on this matter?

Eric
  • 357
  • 1
  • 4
  • 14

4 Answers4

4

You might solve your issue by using a Map and its standard implementation HashMap:

Map<String, Integer> values = new HashMap<String, Integer>();
values.put("a",1);
values.put("b",2);
values.put("c",50);

String array1[] = new String[] {"a","b","c"};

int result = values.get(array1[2]); //result = 50
// or
int result = values.get("c"); //result = 50
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thankyou for your answer, I successfully solve my problem by using your idea. However, I also want to ask if it is possible to get the key by providing the value. For example, using 50 to find out c. – Eric May 29 '12 at 15:19
  • @Eric If you need to be able to do it both ways (from 50 to c and from c to 50) you could use a bi-directional map (the Guava library has one). Alternatively, you can simply have 2 maps - just make sure you put records in both (the 2nd one would be a `Map`. – assylias May 29 '12 at 15:24
  • I have tried to use BiMap as well as HashBiMap. However, I get the error java.lang.NoClassDefFoundError: com.google.common.collect.HashBiMap do you know what the problem is? Thank in advance – Eric May 29 '12 at 18:21
  • You have probably not included the library in your classpath. I suggest you ask a separate question with the details and I'm sure you will get an answer (or just search for `java.lang.NoClassDefFoundError` on Stackoverflow and you will find plenty of answers). – assylias May 29 '12 at 18:24
  • Thanks for your idea . I just ask a new question with more details. http://stackoverflow.com/questions/10804878/guava-java-lang-noclassdeffounderror-com-google-common-collect-hashbimap btw, I have included the library but still get the error message – Eric May 29 '12 at 18:47
  • @Eric I think you have included all the relevant information in your question - You should get good answers. – assylias May 29 '12 at 18:48
1

You can use a HashMap (http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html), is a dictionary like data structure where you can store key-pair values

higuaro
  • 15,730
  • 4
  • 36
  • 43
0

What you're trying to achieve would be better suited to a dynamic/scripting language. Have you considered using a Map instead of multiple varaibles?

Stefan Dorner
  • 171
  • 1
  • 7
  • It's usually a horrible idea in a scripting language, too; they just make it possible to do it. – Wooble May 29 '12 at 13:39
0

In Java, this is not a common approach, such as it would be in scripting languages. You could try to use a Map (ie HashMap), which would enable you to achieve what you want, sort of.

In fact I think it is possible to do exactly what you want using reflection in Java, but I would not go there!

Jochem
  • 2,995
  • 16
  • 18