0

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?

7
  • Like... a pointer? Can you show us in pseudo-code or another language what you're thinking? Commented Mar 5, 2012 at 13:53
  • 2
    Could you provide a bit of pseduo code to explain what you want to do? Commented Mar 5, 2012 at 13:53
  • it could be, what have you tried so far? Commented 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. Commented 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. Commented Mar 5, 2012 at 13:57

5 Answers 5

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I am currently saying this will probably be the best option. There is another for block, which you can do something for each variable in the array. +1 for a good answer.
2

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.

Setting variables by name in Java

Comments

1

You almost certainly want to be using something like a Map or List (essentially a dense map with a small positive int key).

Comments

1

If you don't need to create a new variable with a name, stored in another variable, then reflection is a way to go.

String varName = "x";

Point2D point = new Point(15, 2);
Integer val = (Integer)Point.class.getDeclaredField(varName).get(point);
assert val == 15;

Comments

0

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.

1 Comment

No, not really. I want to say "use the value of x to be the name for Y", and there is like 64 Y's, each with its own value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.