aspose file tools*
The moose likes Game Development and the fly likes Array VS. Vector, which is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Array VS. Vector, which is better?" Watch "Array VS. Vector, which is better?" New topic
Author

Array VS. Vector, which is better?

HaoZhe Xu
Ranch Hand

Joined: Nov 03, 2003
Posts: 222
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.


[url]Olnex.net[/url]
[SCJP 1.2, SCJD, SCWCD]
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
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

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array VS. Vector, which is better?