[Biojava-l] A little (??!!!) stress test.

Hilmar Lapp hlapp@gmx.net
Mon, 03 Jul 2000 12:55:06 +0200


Thomas Down wrote:
> 
> 
> Actually, to be more precise, it turns out that Java runs into
> trouble when working with ArrayLists of 19522579 or more elements.

I don't know whether or not already someone else's figured out, but the actual
problem is quite simple, and (as often :) it's not a bug but a feature,
although the documentation could be more obvious: 

The problem is integer overflow, caused by the way the constructor
ArrayList(Collection c) behaves: it creates the new list with a capacity 10%
greater than the passed collection (which is a documented behaviour), and
calculates the new size by 

	(c.size()*110)/100

Since size() returns a signed integer, overflow will occur at ((2^31)-1)/110,
which is exactly at 19522579, meaning that a list of this size or greater will
result in a negative list size being calculated for the new list.

While I don't understand why size() is defined to return an int instead of a
long, you can circumvent this by not using this constructor, which as far as I
see at present unfortunately means that the fast arraycopy-methods won't be
used internally (the copy-constructor uses c.toArray(Object[]) while
addAll(Collection c) uses an iterator to iterate over c's elements).

	Hilmar