I Know there's a "Vector" class in java, but it seems to be just a simpler ArrayList type of deal, not an actual, mathematical Vector (as in a magnitude and a direction).
Is there any way to do Vector manipulations (particularly addition) in Java? Or am I stuck on my own having to implement it or use a third party module?
The Vector class is considered as a legacy class in Java. It was first introduced with JDK 1.0, and it was later retrofitted to implement the List interface. Even though it is now a member of the Java Collections Framework, it is rarely used in new projects and supported only for backward compatibility.
You should use ArrayList instead of Vector .
The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
Yes, you'll have to write a class or use a library such as JScience
If you are looking to make a vector in 2d space, couldn't you just go with a simple Point2D(x,y)
and let the length of your vector define magnitude?
So that Point2D a = new Point2D(1,1);
has a magnitude of 1.4, and a NE direction. And a Point2D b = new Point2D(2,2);
has the same direction but a magnitude of 2.8...
Addition would then just be: Point2D c = new Point2D(a.x + b.x, a.y + b.y);
In 3d space I would create my own class, or an entirely different data structure depending on you actual problem.
Edit: I hope he has found a solution in the past 3 years..
I don't think there is a built-in way to do vector addition, however I've found a series describing how this could be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With