Is there a way to do this? I'm trying to call an array using a String of it's name
public static void main(String [] args)
{
int [] temp=new int [1];
temp[0]=1;
String a="temp";
System.out.println(a[0]);
}
Is there a way to do this? I'm trying to call an array using a String of it's name
public static void main(String [] args)
{
int [] temp=new int [1];
temp[0]=1;
String a="temp";
System.out.println(a[0]);
}
NO this is not possible as the variable names cannot be declared dynamically in Java.
Try using a HashMap it is similar to what your looking for.
public static void main(String... args) {
HashMap<String, Integer> test = new HashMap<String, Integer>();
test.put("Temp", 1);
test.put("Temp2", 2);
System.out.println(test.get("Temp")); // returns one
HashMap<Integer, String> test2 = new HashMap<Integer, String>();
test2.put(1, "Temp");
test2.put(2, "Temp2");
System.out.println(test2.get(1)); // returns one
}
This is an interesting question if you want to know the differences between Map vs HashMap.
What is the difference between the HashMap and Map objects in Java?