2

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?

3
  • 1
    This isn't an answer but is a clue to where to look. In C# I'd do this with reflection. You have to get the object that represents the property using a string name, and then invoke it. This is because java is compile safe and therefore won't let you do this directly like your doing in your code. Commented Oct 26, 2012 at 18:41
  • what you really should do is put these vectors in a hashmap and key them by "vector1"..."vectorN" strings Commented Oct 26, 2012 at 18:55
  • 1
    @foampile That's the right idea, but the wrong container. An associative array keyed on strings is not what's needed. This is a traditional array with integral indices. Commented Oct 26, 2012 at 18:58

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunatelly, as I've mentioned in the original post, renaming the vectors in a way you're suggesting is not an option for me :-(
You don't rename anything. You declare the arrays in your code. Populate them with references to the original objects. It's the adapter pattern.
3

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]);
   }
}

Comments

0

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);
}

Comments

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.