I want to write a class for mathematical Vectors (holding real numbers). I figured that Vector operations are pretty much the same regardeless of the dimension of the vector, so instead of writing classes like Vector2D
, Vector3D
, Vector4D
, ... I just want to write a Vector
class.
Now the problem is that I can't multiply a 2D vector with a 4D one, so I thought about a field dimension
. But now I had to check it for every operation so I asked myself if I can do better than that. This is were generics came to my mind. But then again I have to do something like Vector<? extends Dimension>
, where Dimension
is simply a base class of Dimension.One
, Dimension.Two
and so on, which means, I have to write a Dimension
class for every dimension I want to use vectos in.
So my question:
Is there a way of writing one class for vectors of arbitrary dimension without having to check the dimension at runtime?