0

There is a C code which I have to change it to Java. In this code there are a lot of vectors like Vector2 and Vector3. I know I can define arrays or lists for them in Java language But I want to ask is there any predefined structure or class in Java which work as the same as vector2 or vector3?

For example Point in java is like a vector2 which 2 double variable but you can't store any other data type in Point.

Taher
  • 1,185
  • 3
  • 18
  • 36

3 Answers3

2

If this is about vector math, you probably want to use a vector math library, e.g.

Otherwise, you may want to use java.util.ArrayList, which is the closest match for c++ stl vectors. Don't use the java Vector class, it's only maintained for compatibility with older code.

For fixed size vectors, you may just want to use Java arrays (as suggested in a different answer).

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
0

Have a look at java.util.ArrayList. It works well as a variable-length container. There's a java.util.Vector class, but it's better not to use that for a variety of reasons.

If you need to perform geometric operations on your vectors (addition, multiplication, projection, etc) then you will need to look at other options (see here for a start).

Community
  • 1
  • 1
mpenkov
  • 21,621
  • 10
  • 84
  • 126
  • 1. The Vector class in Java resembles a List, that's not what the OP is looking for. 2. ArrayList is far better than Vector (but still not what the OP is looking for). – Simon Forsberg Nov 10 '13 at 13:49
  • And how exactly do you know what the OP is looking for? – mpenkov Nov 10 '13 at 13:49
  • 1
    Well actually I'm starting to have doubts myself, since I saw "but you can't store any other data type in Point". Honestly I'm not sure what he wants anymore. Therefore, I removed my downvote but unfortunately I can't really upvote either. – Simon Forsberg Nov 10 '13 at 13:51
  • 1
    As stated in Stefan Haustein's answer, java.util.Vector is still not a good idea. – Simon Forsberg Nov 10 '13 at 13:56
  • Yeah, thanks for pointing that out. I didn't know that before you mentioned it. – mpenkov Nov 10 '13 at 13:57
0

Object[2] is an array of 2 elements that will store any object reference. Or there are several different List classes in java.util (not sure why so many are needed) that will store any object and are variable length. To store, say, a double as an object so it can co-exist with other objects in an array, use new Double(doubleValue).

Hot Licks
  • 47,103
  • 17
  • 93
  • 151