Java Reference
In-Depth Information
Notice that I have ignored one oft-quoted bit of advice that recommends giving a good initial
estimate on the size of the ArrayList . I did time it that way as well; in this example, it made
a difference of less than 4% in the total runtime.
The bottom line is that the efficiency of ArrayList is not totally awful compared to arrays.
Obviously there is more overhead in calling a “get” method than in retrieving an element
from an array. The overhead of objects whose methods actually do some computation prob-
ably outweighs the overhead of fetching and storing objects in an ArrayList rather than in
an Array . Unless you are dealing with large numbers of objects, you may not need to worry
about it. Vector is slightly slower but still only about two-thirds the speed of the original ar-
ray version. If you are concerned about the time, once the “finished” size of the ArrayList
is known, you can convert the ArrayList to an array (see Converting a Collection to an Ar-
ray ).
[ 27 ] You could copy it yourself using a for loop if you wish, but System.arrayCopy( ) is likely to be
faster because it's implemented in native code.
[ 28 ] Both List and Set extend Collection .
 
 
Search WWH ::




Custom Search