user1777928
user1777928

Reputation: 111

using object name as variable in a loop

I have a piece of Java code, where there are Vectors defined as Vector1, Vector2, Vector3,.....VectorN and oldVector1, oldVector2, oldVector3,...oldVectorN.

I need to write a loop that runs over all of these vectors and computes the scalar products of each combination of Vector"i" with oldVector"j".

Actually I know that the best way would be to replace the individual vectors with one array containing all the vectors and work with the array.

However I'm not allowed to touch the rest of the code and the definition of the Vectors as separate objects need to be kept.

How can I do something like this?

for (i = 1; i < 10; i++) {
  for (j = 1 ; i < 10; j++) {
    result[i][j] = dotproduct(Vector"i", oldVector"j");
  }
}

Basically, is there any way in Javahow to construct the variable name similar like a string, e.g. e.g. "Vector"+i?

Upvotes: 2

Views: 111

Answers (3)

mdgeorge
mdgeorge

Reputation: 360

It depends if those references are local variables or fields. If they are local variables, you are probably out of luck. I would probably either write stick them into an array or collection as the other answers suggest, or (if the code is simple or can be put in a function) just duplicate it. That's smelly but the problem is smelly in the first place.

If the references are fields of an object, then you might consider looping over them using reflection. The idea is to use the containing object's class to resolve the field names. The code would read something like the following:

ContainingType containingObject = ...;
for (int i = 1; i < 10; i++) for (int j = 1; j < 10; j++) {
    Field vectorField    = ContainingType.class.getField("vector"    + i);
    Field oldVectorField = ContainingType.class.getField("oldVector" + j);

    Vector vector    = (Vector) vectorIField.get(containingObject);
    Vector oldVector = (Vector) oldVectorField.get(containingObject);
    result[i][j] = dotProduct(vector,oldVector);
}

Upvotes: 0

Jacob Mattison
Jacob Mattison

Reputation: 51052

Can you add in an intermediate collection?

For example, you could do

Vector[] vectors = new Vector[];
Vector[] oldVectors = new Vector[];

vectors[1] = Vector1;
vectors[2] = Vector2;
... (etc.)

oldVectors[1] = oldVector1;
oldVectors[2] = oldVector2;

for (int i=1; i<10; i++) }
   for (int j=1; j<10; j++) {
      result[i][j] = dotproduct(vectors[i], oldVectors[j]);
   }
}

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612794

Instead of naming your objects Vector1, Vector2, Vector3 etc. you should put them in an array. Then you can refer to them using v[i].

You mention this approach in your question and say that you can't use it. But you can. You just need to add a layer between the old code an the new. Initialise the array like this:

v[1] = Vector1;
v[2] = Vector2;
// etc.

Now you can write code that uses array indexing, but still stands on top of the code which you are not allowed to modify.

Upvotes: 3

Related Questions