-4

I have 10 variable declared which are having index at the end.

var_1 = 212; 
var_2 = 343;
var_3 = 122221;
var_4 = 45;
var_5 = 11;
var_6 = 98;
var_7 = 323;
var_8 = 32;
var_9 = 45;
var_10 = 45;

I want to fetch the value of index (var_5) or any index. How do i achieve that dynamically using Java. I want to pass the index 5 and it should return 11.

Anmol Parida
  • 532
  • 4
  • 14
  • 5
    This is what arrays are for. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Andy Turner May 23 '21 at 08:06
  • you could use a `Map`. And extract a value by a key – AlexElin May 23 '21 at 08:07
  • Please explain @AndyTurner – Anmol Parida May 23 '21 at 08:07
  • @AndyTurner This is a small representation of a bigger problem. The indexes are just representations that i entered for ease of understanding. Don't rush into criticising. – Anmol Parida May 23 '21 at 08:11
  • Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – kaya3 May 23 '21 at 08:11
  • 5
    @AnmolParida don't rush into assuming I am criticizing. If this is part of a bigger problem, you need to explain what that is: for what you've written, arrays are the language feature you require. – Andy Turner May 23 '21 at 08:14

3 Answers3

2

What are you really trying to do? That is, why do you need this?

To answer your question: it depends.

If the variables are class members, that you get their value through reflection:

Field field = getClass().getDeclaredField("var_" + index);
Object fieldValue = field.get(this);
int intVlaue = ((Integer)fieldValue).intValue;

If the variables are local variable, you cannot read them in this way, because names of locale variables are not present anymore in the class file.

What you probably really need is an array:

int[] vars = new int[10] { 212, 343, ... };

int value = vars[index - 1]; // Arrays are 0-based.

or, if you only have a value for a few indices or your indices are not integers, a Map:

Map<Integer, Integer> vars = new HashMap<>();
vars.put(1, 212);
vars.put(2, 343);
...

int value = vars.get(index);
Hoopje
  • 11,820
  • 6
  • 29
  • 47
2

This can be easily achieved with the aid of an array in Java.

Sample code snippet.

int[] array = new int[]{ 212,343,122221,45,11,98,323,32,45,45};
int fetchIndex = 5;
if(fetchIndex >0){
System.out.println(arr[fetchIndex-1] );//Since the array indexes are starting from 0 you need to always decrement the array index value you are trying to fetch by one
}

Hasindu Dahanayake
  • 1,175
  • 1
  • 7
  • 25
0

Assuming that an array does not solve your issue (or an instance of Map when the index is not just a number), and further assuming that the var_* variables are in fact class members, you can consider to use Reflection:

var field = getClass().getDeclaredField( String.format( "var_%d", index ) );
var value = ((Integer) field.get( this )).intValue();

Of course I omitted all the mandatory error handling!!

But if your variables are local variables, pure Java does not provide any means to get the values in the way you describe. Perhaps you can write some JNI code or handcraft some Bytecode that does the job, but that's no longer Java in my eyes.

tquadrat
  • 1,653
  • 12
  • 16