For those concerned with speed (I am a speed whore! process speed that is, not the illegal substance) I did a crude test myself a few weeks back to see which who wins out:
Note that I've built my own "Object" (in this case "Entity") class with its own array handlers. I wanted to see if arrays by themselves can be faster dynamically, or if built-in ArrayLists are faster.
The answer somewhat surprised me....
Code:
...
static int resolution = 1000000;
void draw(){
EntityList entityList = new EntityList();
ArrayList arrayList = new ArrayList();
Vector vector = new Vector();
float timer = 0;
Entity entity = new Entity();
println("adding to array");
timer = millis();
for(int i=0; i<resolution; i++){
entityList.push( entity );
}
timer = (millis() - timer);
println( timer );
timer = millis();
for(int i=0; i<resolution; i++){
arrayList.add( entity );
}
timer = (millis() - timer);
println( timer );
timer = millis();
for(int i=0; i<resolution; i++){
vector.add( entity );
}
timer = (millis() - timer);
println( timer );
try{ Thread.sleep(1000); } catch(Exception e){;}
println("traversing array");
timer = millis();
for(int i=0; i<resolution; i++){
Entity e = entityList.getEntity(i);
}
timer = (millis() - timer);
println( timer );
timer = millis();
for(int i=0; i<resolution; i++){
Entity e = (Entity) arrayList.get(i);
}
timer = (millis() - timer);
println( timer );
timer = millis();
for(int i=0; i<resolution; i++){
Entity e = (Entity) vector.get(i);
}
timer = (millis() - timer);
println( timer );
}
// Entities can be anything.
// They can be a process, event, game timer, particle, space ship.
// All entities will have a think() in which they do computational work.
class Entity{
.....
From my experiment:
Code:
// Test results...
//
// adding to array
// entitylist: 81.0 ms
// arraylist: 45.0 ms
// vector: 158.0 ms
//
// traversing array
// entitylist: 10.0 ms
// arraylist: 14.0 ms
// vector: 58.0 ms
//
// holy shit...
// EntityList's only advantage is that it's slightly better than arraylist for traversal.
// Other than that, arraylists seem pretty damned fast.
// Vector is a slow bugger.
Array traversal via normal good ol fashioned arrays is simply faster, by a tiny margin.
When adding objects though, ArrayLists still beat out my "optimized" version of object insertion. My version also includes "smart" dynamic sizing so it multiplies its capacity only when it exceeds some set limit.
Short conclusion is that if you need a list in which the contents are constantly changing, then ArrayLists are still better despite the fact that you COULD implement your own version.
When you have massive amounts of data and need to traverse them all, and the list is mostly unchanging, then arrays are the obvious choice. The reason that ArrayLists are slightly slower than normal arrays is because of the extra cast that you need to make in order to get your object back out.
The sad thing is I used to always use Vectors without knowing the existence of ArrayList. Imagine my delight discovering I could multiply my performance simply by doing a find/replace!
A word of warning, since the OP brought it up:
One would choose Vectors when dealing with Threads and any unsynchronized behavior. However, if you don't expect your list to change asynchronously, then by all means use ArrayLists.