-
String named variables
How do you go about initializing an object variable´s name with a string?
Like I want to initialize a variable like this:
Code:
Obj ["variable_"+count] = new Obj();
I want to do this because i want to access it from an array with its string name instead of numbers. Like id call the element from the array like this:
Code:
Obj retrievedObj = array["variable_100"];
?????
-
The reason why I need it to work like this is because im working with the A* Algorithm for pathfinding and in one of the scenarios i have to check if some node has already exists and thus inspected and if i do this:
Code:
if(array["variable_"+INTVAR] != null){ //then i have inspected it aready
}
If there is some other fast way to look for a certain element in an array, that would be helpful...
-
You can't and what's more, you don't want to. If you want to associate an object with a String, then consider using a Map such as a HashMap. If you want to associate an object with a numeric index of contiguous numbers such as a counter, then just use a simple array, or for more flexibility, and ArrayList.
-
Okay ill look into it thanks