Can I use variables in Java to name variables?

What I want to do is have a loop each time to name a certain number of variables Therefore, sometimes when I run the program, this loop will create three variables A1, A2 & A3, but other times it can name more, for example (if this is possible):

for(int i=1; i<=n;i++) {
    int ai = i;
}

So in this case (for I = 1), the name of int will be A1 and contain int 1 This obviously doesn't work, but I wonder if there is a way to achieve this effect - or should I stop hacking and use different data structures?

thank you.

In addition, this is just an example I use it to create arrays

Solution

No, it's impossible Java cannot construct symbols However, you can use it to define variable - size arrays For example:

int[] a = new int[n];
for(int i = 0; i < n; i++) {
    a[i] = i; 
}

This seems to be what you might want

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>