Variable names using another variable?

edited May 2014 in How To...

I'm just wondering if it's possible to use a variable by using another variable in it's name, in the similar fashion of using arrays...so for example

int var1 = 4;
int var2 = 4;
int var3 = 4;
int var4 = 4;

for (int i = 0; i<4; i++){
var(i) = 5;
println( var(i) ) //var1, var2, var3, var4 should now all = 5
}

Sorry if i did something incorrect, I haven't been on any forums for ages! Thanks for any help!

Answers

  • edited May 2014

    It would save a ton of space, otherwise I'll just use array lists

  • int[] var = new int[4];
    
    for( int i=0; i<4; i++){
      var[i] = 5;
      println( "var[" + i + "] is now " + var[i] );
    }
    
  • Each variable has its own tiny portion of independent RAM!
    Altering the value of 1 doesn't interfere w/ the others!

    What I think you want is using arrays. In which 1 variable refers to 1 array object and access its indexed fields using []:

    http://processing.org/reference/Array.html
    http://processing.org/reference/arrayaccess.html

  • "in the similar fashion of using arrays"
    Well, then, just use arrays! No?

    From several variables to arrays

  • I know how to use arrays, just in the particular problem I had It would have been useful to do it this way. I appreciate the help however, yea I ended up just using arraylists which worked fine

  • Answer ✓

    OK. Just to address your initial curiosity: no, it is not possible to do that in Processing / Java. Actually, I have rarely meet languages allowing this kind of trick (perhaps only one, and I don't remember which one).

    You are not the first one to ask this kind of question... :-)
    Note that an alternative, something equivalent, can be done with HashMap, but for a somehow different goal (it is inefficient for primitive values).

  • edited June 2014

    I've finally managed to set those variables via reflexion! Check it out: \m/

    /**
     * Get/Set Fields (v1.14)
     * by GoToLoop (2014/May)
     *
     * forum.processing.org/two/discussion/5065/variable-names-using-another-variable
     * forum.processing.org/two/discussion/5678/loading-class-files-weird-behavior-
     */
    
    import java.lang.reflect.Field;
    
    int var1, var2, var3, var4;
    
    void setup() {
      final Class<? extends PApplet> c = getClass();
    
      final Field[] fields = getFieldsContaining(c, "var");
    
      for (final Field f: fields) {
        changeIntField(f, this, 0100);
        println(readIntField(f, this));
      }
    
      println();
    
      for (int i = 1; i != 5;) {
        final Field f = getField(c, "var" + i++);
    
        if (f != null) {
          changeIntField(f, this, 0x100);
          println(readIntField(f, this));
        }
      }
    
      exit();
    }
    
    static final Field[] getFieldsContaining(final Class c, final CharSequence s) {
      final Field[] fields = c.getDeclaredFields();
      final ArrayList<Field> list = new ArrayList(fields.length);
    
      for (final Field f: fields)  if (f.getName().contains(s))  list.add(f);
    
      return list.toArray(new Field[list.size()]);
    }
    
    static final Field getField(final Class c, final String s) {
      try {
        return c.getDeclaredField(s);
      }
    
      catch (final NoSuchFieldException e) {
        println(e);
        return null;
      }
    }
    
    static final void changeIntField(final Field f, final Object p, final int num) {
      try {
        f.setInt(p, num);
      }
    
      catch (final IllegalAccessException e) {
        println(e);
      }
    }
    
    static final int readIntField(final Field f, final Object p) {
      try {
        return f.getInt(p);
      }
    
      catch (final IllegalAccessException e) {
        println(e);
        return MIN_INT;
      }
    }
    
  • Okay thanks for the help! GoToLoop...I don't even...what is that?

  • It's called reflexion! Through that, we can access any class member such as fields, methods, etc. :-B

    • getFieldsContaining() returns a Field[] containing all fields whose names have the String argument.
    • getField() returns a Field of name String from Class argument.
    • changeIntField() changes a Field of type int to a value from an Object's instance.
    • readIntField() returns Field's current int value from an Object's instance.

    In short, I was able to read & change all fields w/ names starting w/ "var" to a specific value at once! \m/

  • edited May 2014

    A more simplistic example about using those functions: O:-)

    // forum.processing.org/two/discussion/5065/
    // variable-names-using-another-variable
    
    import java.lang.reflect.Field;
    
    int var1, var2, var3, var4;
    
    void setup() {
      // Obtain field w/ name "var3" from current class:
      final Field f = getField(getClass(), "var3");
    
      if (f != null) {
        // Set its value to 256 from an instance of this class:
        changeIntField(f, this, 0x100);
    
        // Display its current changed value now:
        println(readIntField(f, this));
      }
    
      // Re-confirming it's indeed changed to 256:
      println(var3);
    
      exit();
    }
    
Sign In or Register to comment.