Hello everybody.
I am in need of an answer. How can I use the text from a string and use it as a variable's name?

For example:

I have an array: int xyz[] = new int[5];
And say I want to check if it's name is the same as a string:

if("xyz" == my_array's_name):
//do something

And for that matter how can I do the opposite, that is to get a variable's name and store it as a string.

Thank you in advance!

Recommended Answers

All 10 Replies

You can do access things like method or variable (reflection refers to variables as "Fields") names using the Reflection classes in java.lang.reflect
eg the Field class has a getName() that returns the name of the field.
Start with the Class class - that's where you get the Fields and Methods from.

ps: but why do you want to do this?

I have a class that stores a lot of arrays, and their name is important since i want to retrieve the data stored in them by using a local method which should return the values of those arrays by using their name. Hope I explained this right :)

If the names are a known fixed set then you can probably do this more simply with a switch that selects the right array, or, even better, put them all in a Map with the name as key and the array as value.
Otherwize you get the Class object for your class, get the Field using getField(String name), then use get(Object o) on the field to get the object it refers to (the parameter is the instance of your class to use for instance variables). Finally cast the returned Object to the appropriate array. (But map is simpler.)

My arrays aren't fixed, their number and values will be modified quite often.
I am having a hard time trying to do what you suggested (I am not that advanced with java).
Can you plase refraze for a more simple soul to understand? :) (maybe an example?)
Here is something simmilar to what I have:

class ClassName{
public double abc[] = new double[15];
public double def[] = new double[15];
//.....................................etc

public ClassName(){
//Constructor initializes array values
}
public void updateArray(String arrayName) {
//Here is my problem, i want to do something like this:
arrayName[0] = 10.0; //I want arrayName to reffer to the array stored in this class
arrayName[1] = 20.2;
//....................etc
}
}

Thank you for your patience :)

OK, this is fun! The Map is the way to go.
Have a Map that maps a String name to double array. HashMap is the one to use here:

Map<String, double[]> names = new HashMap<String, double[]>();

when you need a new array just add it the map

names.put("Matthew", new double[15]); // new array with name Matthew

then you can just retrieve the one you need

double[] temp = names.get("Matthew");

or use them directly

names.get("Matthew")[0] = 12.3;

I'll be... it worked! :)
Thank you very much good sir

Excellent! Please mark this solved so others will know.

Aaaa, not quite there yet... got over excited.

So, by using what you said, I've got a temporary double array that has the same values as one of the arrays stored, update it the way I want using the method, but how do I store the new array values to the old array.
At the end, I need something like:

double temp = names.get("Matthew");
temp[0] = new_value;
names.get("Matthew");

Which I know is wrong, but this explains what I need

I've

got a temporary double array that has the same values as one of the arrays stored

No, that's completely wrong. (Beginners in Java usually hit this particular confusion sooner or later. temp is NOT an array, despite the misleading syntax. temp is a reference (a.k.a. pointer) to an array.
Your line 1 above creates a variable temp that refers to exactly the same array as names.get("Matthew") . When you update temp[i] that is exactly the same as updating names.get("Matthew")[i] . There is only one array, but there are two ways to refer to it.

Thnak you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.