• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Array VS. Vector, which is better?

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 26275
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Liar, liar, pants on fire! refreshing plug:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic