I have the following code:
public static void main(String[] args) {
String a1 = "aa";
String a2 = "bb";
String a3 = "cc";
String a4 = "dd";
for(int i = 0; i<3; i++){
String a5 = ("a"+ i);
System.out.println("a"+i +"= "+a5);
}
}
I would like to know how you can somehow print the value of the variables above in for
that way or you can't because there it prints a1, a2 and so on and what I want is to print the value of a1
that is aa
.
Thanks for the help in advance
What you are trying to do is evaluate
"a"+ i
, as if it were a variable ega1
,a2
, etc. This is not possible:If you want to get the data printed, one option is to create an array containing type elements
String
and add these values.Remember that you can access the elements of an array by their index. For example:
elementos[0]
will have the value"aa"
.elementos[1]
will have the value"bb"
.elementos[2]
will have the value"cc"
.elementos[3]
will have the value"dd"
.To print them you would do it this way, accessing the values inside the array:
In a programming language like Java, what you are trying to do is not possible, and it doesn't make sense either .
The reason is that variable names only make sense at compile time, they are not available at run time, not even through reflection.
On the line
String a5 = ("a"+ i);
where you build the variable name, it's a simple string that will ultimately be printed as such, on this line:System.out.println("a"+i +"= "+a5);
so you get"a1"
,"a2"
,"a3"
and"a4"
.By the time those lines are being executed, the java compiler will have long since converted the source code into bytecode that does not preserve variable names and such variables are referenced via stack positions or cpu registers.
Code
Result
Explanation
Most programming languages have a concept known as Reflection , this consists of being able to access certain elements, functions, methods, etc. Using references to them through their names.
What does this mean?
This suggests to us that there are languages in which you could easily do something like:
Cases like these exist in PHP, C++, Java, Etc.
let's get down to business
Using the
getFields
Java method, we can access all the variables whose visibility is public , therefore we make use of said function on the classClase
obtaining from it all the declared variables, for this casea1,a2,a3,a4
and there being able to obtain their values later.Yes it can be done from a conceptual point of view.
You want to be able to associate values ("aa", "bb" ..) to some "variables" to which you give names ("a1", "a2" ...). And you want to be able to access those "variables" by building their name dynamically.
I put variables in quotes because in the strict case of variables inside a method, which are the ones that appear in your example, it is not possible, as far as I know, not even with reflection. But it is possible to have a mechanism that allows you to associate some values to some names. That mechanism is called MAP .
A map is an associative container. It comes to be like an array. But in an array the access index can only be an integer, in a map it can be any data type, and in maps it is often called a key instead of an index.
With the following code we create a map that associates values of the TipoValor type to keys of the TipoClave type.
In our case we will use String as KeyType and String as ValueType.
To assign (or using map terminology associate ) a value to a key we use put. The first four lines of the original author's main would look like this:
To read the value associated with a key we use the get method. The original author's code using maps would be:
Hello, your problem is that you are replacing the variable a1, a2
}