• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array VS. Vector, which is better?

 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a map editor for a game, the structure is like this:
data[L][X][Y]
L: level index
X: x position
Y: y position
assume L, X, Y are constants, if i write it in this way:
Vector LData;
Vector XData = LData.get(Xindex)
Vector YData = XData.get(Yindex)
so everything in array data is included in a vector LData, but i wonder:
1. which way is faster?
2. which way uses less memory?
2. which way do you prefer?
thank you.
 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector uses Arrays internally. The main advantage of a Vector compared to an Array is it's automatical increase of capacity. An Array keeps it's size once created, a Vector does not.
Because a map's size is not likely to change, I'd use arrays for better performance because I wouldn't need Vector's advantages.

Regards,
Michael Herrmann
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'd look at ArrayList over Vector. Vector is the old collections, that have been replaced. You can still use them, however, there is a heavier cost.

Mark
 
Tim Holloway
Saloon Keeper
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays can be expected to have the fastest access (primitives functions) and least amount of memory usage. However they are fixed in size.

If you need something that dynamically resizes, a collection object is more useful.

These days, ArrayList is referred over Vector because Vector is thread-safe and that's extra overhead (unless you actually [i]need/[i] to be thread-safe.

In a lot of cases, I'm building objects from some sort of input channel, so I use a vector (ArrayList) to allow for expansion, then convert to an array when I'm done. This is very easy in Java:

List stuffList = new ArrayList();
// put stuff in list
MyObject[] stuff = (MyObject[]) stuff.toArray(new MyObject[stuffList.size()]);

Java 5 makes this simpler, since generics can be used instead of casting.

Note that if you pass an array into the toArray method, the same array will be returned (populated) to you. Less overhead that way. The collection's internal array is cloned rather than being itself returned. That way the collection's internal data integrity is maintained.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Holloway:
Arrays can be expected to have the fastest access (primitives functions) and least amount of memory usage. However they are fixed in size.

If you need something that dynamically resizes, a collection object is more useful.

These days, ArrayList is referred over Vector because Vector is thread-safe and that's extra overhead (unless you actually [i]need/[i] to be thread-safe.

In a lot of cases, I'm building objects from some sort of input channel, so I use a vector (ArrayList) to allow for expansion, then convert to an array when I'm done. This is very easy in Java:

List stuffList = new ArrayList();
// put stuff in list
MyObject[] stuff = (MyObject[]) stuff.toArray(new MyObject[stuffList.size()]);

Java 5 makes this simpler, since generics can be used instead of casting.

Note that if you pass an array into the toArray method, the same array will be returned (populated) to you. Less overhead that way. The collection's internal array is cloned rather than being itself returned. That way the collection's internal data integrity is maintained.

If you use the toArray() method as suggested above, the cast isn't even necessary with a few modifications:

As mentioned, the array parameter is populated and returned, so you don't need to capture the return value since the array is passed by reference.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic