Java Reference
In-Depth Information
// }
// better: reallocate, making data structure dynamic
iif ( nDates >= dates . length ) {
Date [] tmp = new
new Date [ dates . length * GROW_FACTOR ];
System . arraycopy ( dates , 0 , tmp , 0 , dates . length );
dates = tmp ; // copies the array reference
// old array will be garbage collected soon...
}
dates [ nDates ++] = c ;
}
System . out . println ( "Final array size = " + dates . length );
}
}
A good guess is necessary; know your data!
Th e growth factor is arbitary; 2 is a good value here but will continue to double exponen-
tially. If Java did type inference, it would not be inconvenient to use a factor like 1.5,
which would mean more allocations at the low end but less explosive growth. You need
to manage this somehow!
This technique works reasonably well for simple or relatively small linear collections of
data. For data with a more variable structure, you probably want to use a more dynamic
approach, as in Like an Array, but More Dynamic .
 
Search WWH ::




Custom Search