Alright, I am not completely sure that I worded the title right, but I want to use an int variable to define another int to go through a method. In other words, I want to be able to have the int/name/thingy variable go through a ++ statement, and then the next variable would go through the method. Is this possible?
-
Like... a pointer? Can you show us in pseudo-code or another language what you're thinking?Kylar– Kylar2012-03-05 13:53:44 +00:00Commented Mar 5, 2012 at 13:53
-
2Could you provide a bit of pseduo code to explain what you want to do?matt freake– matt freake2012-03-05 13:53:49 +00:00Commented Mar 5, 2012 at 13:53
-
it could be, what have you tried so far?Luiggi Mendoza– Luiggi Mendoza2012-03-05 13:54:07 +00:00Commented Mar 5, 2012 at 13:54
-
@Kylar I only really know java... Anyways I am trying to make a virtual grid for a Conways Game of Life thing, and am debating where and how to store the variables.fr00ty_l00ps– fr00ty_l00ps2012-03-05 13:56:17 +00:00Commented Mar 5, 2012 at 13:56
-
And I haven't tried yet, just wanted to know if I would be spinning my wheels or not.fr00ty_l00ps– fr00ty_l00ps2012-03-05 13:57:19 +00:00Commented Mar 5, 2012 at 13:57
5 Answers
An int array might solve your problem. The array stores your ints ("variables"), another one runs throught the index (your ++ operation):
int[] values = loadValuesInArray(); // some magic to get the populated array
for (int i = 0; i < values.length; i++) {
myMagicMethod(values[i]); // calling the method with int values
}
If you need named variables, then you can use a map:
Map<String, Integer> variables = new HashMap<String, Integer>();
variables.put("a", 1);
variables.put("b", -10);
variables.put("c", 25);
myMagicMethod(variables.get("b")); // calls method with value from "variable" b
1 Comment
You can use reflection.
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
http://www.java2s.com/Tutorial/Java/0125__Reflection/Catalog0125__Reflection.htm
or see to have an idea.
Comments
You say "I want to use an int variable to define another int" do you mean something like
int a = 0; //declare a new variable a of type int and assign it to zero
a b = 0; //declare a new variable b of type a which is an int and assign it to zero
From what you said, that is what it appears you are saying. If that is the case, I do not think you can do that. My memory says that there is a method that allows you to determine the type of an object, but to use that as a declaration of another variable... I don't know if you can do that.