Re: A list of ever member in a class.



Daniel Gee <zefria@xxxxxxxxx> wrote in news:1177371058.868913.54070
@n76g2000hsh.googlegroups.com:


public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable,
java.io.Serializable
{
private transient E[] elementData;
...
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}
...
}

Does the casting make all the difference?


Yes. Here they are creating an array of Objects, and casting it to an
array of Es, which to the compiler is quite different from creating an
array of Es directly.

As Tom Hawtin said, don't try this at home.

I've never liked generics, and seeing this code doesn't help. If the
people implementing ArrayList have to resort to this kind of tricks, maybe
it should've made them wonder at the validity of generics.
Of course copying the syntax from C++ templates (which btw *are* a well
thought out feature, albeit with scary-looking syntax) only makes things
worse.

Btw just because I don't like generics doesn't mean I don't use them. As
long as you know what to expect and what not they're useful in their own
right. I just think they could've been designed better.

Zero
.