Ignore variable names?

Ok, so for what I am doing, I need to increase the name of the variables, for example, int Taco1 = 23432 ..... int Taco2 = 234235656 ..... int Taco3 = 11111 .......

But instead, I need a variable like

int X = 0; some method with loop or recursion() int Taco(X) = bla bla bla x++ 

Attempting to get variable names for an automatic name independently increases by 1 each time, so they are not overwritten. If this is not possible, then my apologies.

+4
source share
5 answers

You cannot do this in Java and, more importantly, you do not want to do this, since that is not how Java works. In fact, variable names are not as important as you think, and they hardly even exist in compiled code. What is much more important is that you can get a link to your objects as simple and reliable as possible. This may include an array, an ArrayList (probably what you want here), a LinkedList, a map such as a HashMap, Set, and other types of collections.

For instance:

 List<Taco> tacoList = new ArrayList<Taco>(); for (int i = 0; i < MAX_TACOS; i++) { tacoList.add(new Taco(i)); } 
+13
source

In fact, it is not possible to generate identifier names based on a variable. Maybe you need an array:

 int[] Taco = new int[ 15 /*some appropiate upper limit*/ ]; Taco[X] = bla bla bla; 

Find basic information on the Internet about which arrays and how they work.

+5
source

Use int[] or List<Integer> :

 int[] tacos = new int[numberOfTacos]; // in some loop or whatever tacos[x] = someValue; 
+2
source

use an int array. say int taco[50] ; and you can refer to each location as taco[0] , taco[1] , etc.

+1
source

I TacoX will be an integer, I would create an array from X int. If the maximum number is 10, we have:

 int[] taco = new int[10]; 

Then, to change / read tacoX, you just look at taco [X]

0
source

All Articles