Well, an arraylist uses an array internally. So if you work on the basis
that "a thing on a thing is always slower than a thing" then yes it
will be
slower, but for reference types I wouldn't have thought that
performance
difference was a huge issue (although you will have to perform casting
when
the objects are retrieved).
Arrays are faster for value types as value types have to be boxed when they
are added to the arraylist (I can't wait for the List<T> generic from
version
2.0). So if you are storing value types and you know how big the
collection is
going to be before hand (before you need to allocate it not
necessarily at
design time) then use an array.
To be honest though, the main thing you get from an array with
reference types is compile time type checking. The compiler will spot
if you try to put something in there that is not of the correct type.
So for me, arrays are cleaner and more efficient but lack flexibility.
ArrayList is flexible, but that comes at a cost (although that cost is
often worth it in the face of the extra flexibility you get).
This is one of the best reasons I've ever read for preferring one
structure over another when being able insert / add to the list of
items is not needed after initial allocation. The other reasons being
type safety (a big plus as it often saves you heaps of debugging time,
leading to cheaper applications) and not having to cast on retrieval
(which clutters up your code). These latter reasons will go away with
generic types in Whidbey, but Richard's reason will still stand.
I don't know if this is Bob's case, but in every shop I've worked in
there have been one or more programmers concerned about "efficiency".
Usually they made foolish design decisions in order to save a few
clock cycles in programs that were spending 99% of their time doing
other things. For example, one fellow used arrays instead of hash
tables (when hash tables were clearly superior) because arrays were
"more efficient" than dynamic memory structures... in a program that
then put together a huge SQL query and headed off to a database for
half an hour. The program was chock-a-block with awkward code that
achieved a run time of 30 minutes and 2 milliseconds instead of 30
minutes and 3 milliseconds. It was ridiculous.
If you are building software with hard real-time deadlines (telephone
switches, medical equipment, etc), or if you are doing hugely
CPU-intensive things (ray tracing, game programming, etc) then you
should be looking to lab reports for efficiency and speed of certain
constructs within .NET. (But then I have to ask why you're using a
language with a garbage collector for applications where every CPU
cycle counts.) Otherwise, you're much better off to write the program,
profile it, find the bottlenecks, and concentrate your efforts on
improving the speed of what you find.
Arrays are no doubt quicker than ArrayList, but the difference is
unlikely to be so great as to dwarf performance bottlenecks in other
parts of your program, unless, as I said, you're writing one of the
few kinds of apps that spends the bulk of its time in the CPU.