Re: A list of ever member in a class.
<nitpicking>
</nitpicking>
Ah, right. I get those names often mixed up because the teacher who
gave me the first formal introduction to the subject was actually a C+
+ programmer who wasn't used to teaching in Java.
Right then, "generics" it is.
public class MemberHolder<T>
{
public T[] mymembers;
public MemberHolder(int numberToHold)
{
mymembers = new T[numberToHold];
}
}
Unfortunately this won't work. Mixing generics and arrays is never a
good idea, and in this case the compiler won't even let you: you can't
create an array of generic types. There's a section in the JLS
explaining why this is, but I'm afraid I can't remember where exactly.
but...... sections from the source for java.util.ArrayList itself
read:
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?