Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you do Vector addition in Java, natively?

Tags:

java

math

vector

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?

like image 565
J.R. Avatar asked Oct 27 '09 17:10

J.R.


People also ask

Is Vector still used in Java?

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.

What is the alternative of Vector in Java?

You should use ArrayList instead of Vector .

Is there any Vector in Java?

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.


3 Answers

Yes, you'll have to write a class or use a library such as JScience

like image 193
dfa Avatar answered Sep 22 '22 21:09

dfa


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..

like image 4
Stegger Avatar answered Sep 23 '22 21:09

Stegger


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.

like image 3
luvieere Avatar answered Sep 23 '22 21:09

luvieere