Re: Lists of size Integer.MAX_VALUE
Piotr Kobzda wrote:
Lew wrote:
Suppose we have a List 'someList' containing exactly Integer.MAX_VALUE
elements and we
someList.add( element );
or
someList.add( Integer.MAX_VALUE, element );
According to the Javadocs, this is legal, but how then could we
reference the last element of the list?
someList.listIterator(Integer.MAX_VALUE).next();
Of course, it requires the implementation of ListIterator which don't
fully support its interface (in particular previousIndex()/nextIndex()
methods cannot be fully supported).
Safer approach (in terms of achieving working solution) would be:
lastElement = null;
for(Iterator it = someList.iterator();
it.hasNext(); lastElement = it.next());
But it's poor from the performance point of view...
In fact, the same performance problem applies to the ListLterator's use
(a list must be a bit bigger only to observe that, for example
2*Integer.MAX_VALUE, or more, elements...).
So, currently defined List interface disallows the efficient working
with a "large lists" IMHO.
Some additional methods are needed to support that, for example a list
implementing the following interface would be better:
public interface LargeList<T> extends List<T> {
/** Returns a view of the portion of this list between the
specified fromIndex, inclusive, and this list's last element, inclusive. */
List<T> subList(int fromIndex);
}
Unfortunately, most of the current uses of lists assumes that a maximum
list size is Integer.MAX_VALUE, so the above-like large list won't work
as expected with other libraries (e.g. Collections.reverse() is useless
with such a list...).
Conclusion: large lists are impractical.
piotr